【问题标题】:Random number generator in c with max and min [duplicate]c中具有最大值和最小值的随机数生成器[重复]
【发布时间】:2023-04-10 13:45:01
【问题描述】:

可能重复:
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 的随机数并用它们填满一个板 [40] 有什么想法吗?

【问题讨论】:

    标签: c random numbers generator


    【解决方案1】:
    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);  
    }
    

    【讨论】:

    • 哦,非常感谢,我怎样才能做到 40 次并保存所有这些随机数?
    • 最后我怎么能看到数字 1-3 出现了多少次,数字 4-6 出现了多少次?
    • 我如何打印数字 1-3 出现了多少次以及数字 4-6 出现了多少次?
    猜你喜欢
    • 2011-08-28
    • 1970-01-01
    • 1970-01-01
    • 2018-11-10
    • 1970-01-01
    • 1970-01-01
    • 2017-05-11
    • 2021-02-19
    • 2023-03-25
    相关资源
    最近更新 更多