[C]随机数生成

 生成骰子数

#include <stdio.h>
#include <time.h> //time()
#include <stdlib.h> //srand(), rand()
void main() {
    srand(time(NULL));
    for(int i = 0; i < 10; ++i) {
        printf("%d
", rand()%6 + 1); //生成骰子数,[1,6]
    }
}

奇数与偶数

#include <stdio.h>
#include <time.h> //time()
#include <stdlib.h> //srand(), rand()
void main() {
    srand(time(NULL));
    for(int i = 0; i < 10; ++i) {
        long unsigned int r = rand();
        if (r % 2) { //奇数
            fprintf(stderr, "%ld
", r);
        } else {
            fprintf(stdout, "%ld
", r);
        }
    }
}

编译运行:

gcc random.c && ./a.out > even.txt 2> odd.txt
原文地址:https://www.cnblogs.com/profesor/p/13212297.html