C语言 在c中生成1到10之间的随机数

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/17846212/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-02 07:00:27  来源:igfitidea点击:

generate a random number between 1 and 10 in c

crandom

提问by Nisarg

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

int main()

{
    int randomnumber;
    randomnumber = rand() % 10;
    printf("%d\n", randomnumber);
    return 0;
}

This is a simple program where randomnumber is an uninitialized int variable that is meant to be printed as a random number between 1 and 10. However, it always prints the same number whenever I run over and over again. Can somebody please help and tell me why this is happening? Thank you.

这是一个简单的程序,其中 randomnumber 是一个未初始化的 int 变量,旨在作为 1 到 10 之间的随机数打印。但是,每当我一遍又一遍地运行时,它总是打印相同的数字。有人可以帮忙告诉我为什么会这样吗?谢谢你。

采纳答案by ouah

You need a different seed at every execution.

每次执行都需要不同的种子。

You can start to call at the beginning of your program:

您可以在程序开始时开始调用:

srand(time(NULL));

Note that % 10yields a result from 0to 9and not from 1to 10: just add 1to your %expression to get 1to 10.

需要注意的是% 10产生从结果09,而不是来自110:只需添加1到您的%表达让110

回答by Byusa

srand(time(NULL));

int nRandonNumber = rand()%((nMax+1)-nMin) + nMin;
printf("%d\n",nRandonNumber);

回答by dang

Here is a ready to run source code for random number generator using c taken from this site: http://www.random-number.com/random-number-c/. The implementation here is more general (a function that gets 3 parameters: min,max and number of random numbers to generate)

这是一个准备运行的随机数生成器源代码,使用来自本站点的 c:http: //www.random-number.com/random-number-c/。这里的实现更通用(一个函数,它获取 3 个参数:min、max 和要生成的随机数的数量)

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

// the random function
void RandomNumberGenerator(const int nMin, const int nMax, const int  nNumOfNumsToGenerate)
{
  int nRandonNumber = 0;
  for (int i = 0; i < nNumOfNumsToGenerate; i++)
  {
    nRandonNumber = rand()%(nMax-nMin) + nMin;
    printf("%d ", nRandonNumber);
  }
  printf("\n");
}

void main()
{
  srand(time(NULL));
  RandomNumberGenerator(1,70,5);
}

回答by Macattack

You need to seed the random number generator, from man 3 rand

您需要为随机数生成器提供种子,从 man 3 rand

If no seed value is provided, the rand() function is automatically seeded with a value of 1.

如果未提供种子值,则 rand() 函数将自动以 1 值作为种子。

and

The srand() function sets its argument as the seed for a new sequence of pseudo-random integers to be returned by rand(). These sequences are repeatable by calling srand() with the same seed value.

srand() 函数将其参数设置为 rand() 返回的新伪随机整数序列的种子。这些序列可以通过使用相同的种子值调用 srand() 来重复。

e.g.

例如

srand(time(NULL));

回答by Lee Daniel Crocker

Generating a single random number in a program is problematic. Random number generators are only "random" in the sense that repeated invocations produce numbers from a given probability distribution.

在程序中生成单个随机数是有问题的。随机数生成器只是“随机”,因为重复调用会根据给定的概率分布生成数字。

Seeding the RNG won't help, especially if you just seed it from a low-resolution timer. You'll just get numbers that are a hash function of the time, and if you call the program often, they may not change often. You might improve a little bit by using srand(time(NULL) + getpid())(_getpid()on Windows), but that still won't be random.

播种 RNG 无济于事,尤其是如果您只是从低分辨率计时器中播种。您只会得到作为时间散列函数的数字,如果您经常调用该程序,它们可能不会经常更改。您可能会通过使用srand(time(NULL) + getpid())(_getpid()在 Windows 上)来改进一点,但这仍然不是随机的。

The ONLY way to get numbers that are random acrossmultiple invocations of a program is to get them from outside the program. That means using a system service such as /dev/random(Linux) or CryptGenRandom()(Windows), or from a service like random.org.

只有这样,才能让那些随机数跨越多个调用的程序是从程序以外得到他们。这意味着使用诸如/dev/random(Linux) 或CryptGenRandom()(Windows) 之类的系统服务,或来自诸如random.org.