C语言 如何生成一个介于 0 和 1 之间的随机数?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/6218399/
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 08:50:50  来源:igfitidea点击:

How to generate a random number between 0 and 1?

c

提问by user739062

I want to generate random numbers between (0,1). I am trying the following:

我想在(0,1)之间生成随机数。我正在尝试以下操作:

double r2()
{
    return((rand() % 10000) / 10000.0);
}

int SA()
{
    double u;
    u = r2();
}

But it doesn't generate the expected result. How can I fix it?

但它不会产生预期的结果。我该如何解决?

回答by Clifford

In your version rand() % 10000will yield an integer between 0and 9999. Since RAND_MAX may be as little as 32767, and since this is not exactly divisible by 10000 and not large relative to 10000, there will be significant bias in the 'randomness' of the result, moreover, the maximum value will be 0.9999, not 1.0, and you have unnecessarily restricted your values to four decimal places.

在您的版本rand() % 10000中将产生一个介于0和之间的整数9999。由于 RAND_MAX 可能小到 32767,而且由于这不能被 10000 整除,而且相对于 10000 来说也不大,因此结果的“随机性”会有很大的偏差,而且最大值将为 0.9999,而不是 1.0 ,并且您不必要地将您的值限制为四位小数。

It is simple arithmetic, a random number divided by the maximum possible random number will yield a number from 0 to 1 inclusive, while utilising the full resolution and distribution of the RNG

这是一个简单的算法,一个随机数除以最大可能的随机数将产生一个从 0 到 1 的数字,同时利用 RNG 的完整分辨率和分布

double r2()
{
    return (double)rand() / (double)RAND_MAX ;
}

Use (double)rand() / (double)((unsigned)RAND_MAX + 1)if exclusion of 1.0 was intentional.

(double)rand() / (double)((unsigned)RAND_MAX + 1)如果有意排除 1.0,则使用。

回答by John Bode

Here's a general procedure for producing a random number in a specified range:

以下是在指定范围内生成随机数的一般过程:

int randInRange(int min, int max)
{
  return min + (int) (rand() / (double) (RAND_MAX + 1) * (max - min + 1));
}

Depending on the PRNG algorithm being used, the %operator may result in a very non-random sequence of numbers.

根据所使用的 PRNG 算法,%运算符可能会产生非常随机的数字序列。

回答by andreea

It seems to me you have not called srand first. Usage example here.

在我看来,你没有先调用 srand。这里的用法示例。

回答by Stealth Rabbi

Set the seed using srand(). Also, you're not specifying the max value in rand(), so it's using RAND_MAX. I'm not sure if it's actually 10000... why not just specify it. Although, we don't know what your "expected results" are. It's a random number generator. What are you expecting, and what are you seeing?

使用 srand() 设置种子。此外,您没有在 rand() 中指定最大值,因此它使用的是 RAND_MAX。我不确定它是否真的是 10000 ......为什么不直接指定它。虽然,我们不知道您的“预期结果”是什么。这是一个随机数生成器。你在期待什么,你在看什么?

As noted in another comment, SA() isn't returning anything explicitly.

正如另一条评论中所述, SA() 没有明确返回任何内容。

http://pubs.opengroup.org/onlinepubs/009695399/functions/rand.htmlhttp://www.thinkage.ca/english/gcos/expl/c/lib/rand.html

http://pubs.opengroup.org/onlinepubs/009695399/functions/rand.html http://www.thinkage.ca/english/gcos/expl/c/lib/rand.html

Edit: From Generating random number between [-1, 1] in C?((float)rand())/RAND_MAXreturns a floating-point number in [0,1]

编辑:从C 中的 [-1, 1] 之间生成随机数?((float)rand())/RAND_MAX返回 [0,1] 中的浮点数

回答by Sho arata

double r2()
{
   return (rand() % 10001) / 10000.0;
}

回答by Hiesso

Have you tried with: replacing ((rand() % 10000) / 10000.0), with:(rand() % 2). This worked for me!

您是否尝试过:替换 ((rand() % 10000) / 10000.0), with:(rand() % 2)。这对我有用!

So you could do something like this:

所以你可以做这样的事情:

double r2()
{
     srand(time(NULL));
     return(rand() % 2);
}