c - c 中的随机数生成器,具有最大值和最小值

标签 c random numbers generator

Possible Duplicate:
In C, how do I get a specific range of numbers from rand()?

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

int main(int argc, char** argv) {


srand(time(NULL));
int r = rand();

printf("%d\n", r);

return (EXIT_SUCCESS); }

我有这段代码,我想生成 1-6 之间的随机数,并将它们填入一 block 板 [40] 有什么想法吗?

最佳答案

int main(int argc, char** argv) {
    int i;
    int array[40];
    srand(time(NULL));
    for (i=0; i<40; i++)
        array[i] = rand()%6 + 1
    return (EXIT_SUCCESS);  
}

关于c - c 中的随机数生成器,具有最大值和最小值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10999657/

相关文章:

CS50 维吉内尔问题。越界访问、未定义的行为

c++ - 如何使用Mongoose(一个http服务器)来服务不同的请求?

c++ - C++11 中分布的多态性

JAVA:泛型编译时间常量

c - fscanf - 在将整数存储到数组之前执行算术运算

c - 二维数组中 c 语言的螺旋矩阵

java - 快速生成 ID,并具有很高的唯一性

c++ - C++ 中的线性同余生成器、运算符 & 和 >>

c - 在C中打印出随机数数组

count - 莱克斯 : print line numbers