循环生成随机数的代码示例:

#include <stdio.h>
#include <stdlib.h>
#include <time.h>

int main() {
    srand(time(NULL)); // 初始化随机数种子
    for (int i = 0; i < 10; i++) {
        printf('%d\n', rand()); // 输出随机数
    }
    return 0;
}

使用 rand 函数生成随机数需要先调用 srand 函数初始化随机数种子。可以使用 time 函数获取当前时间作为种子,然后在循环中调用 rand 函数生成随机数。

C语言 for 循环生成随机数

原文地址: https://www.cveoy.top/t/topic/jsyL 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录