C语言 rand() 与 time() 问题一起播种

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

rand() seeding with time() problem

ctimerandom

提问by Ock

I'm having difficulty figuring out how to use rand() and seeding it with time() using Xcode. I want to generate random decimal numbers between 0 and 1.

我很难弄清楚如何使用 rand() 并使用 Xcode 将其与 time() 一起播种。我想生成 0 到 1 之间的随机十进制数。

The code gives me seemingly random numbers for elements 1 and 2, but element 0 is always somewhere around 0.077. Any ideas why this would be?

该代码为我提供了元素 1 和 2 的看似随机数,但元素 0 始终在 0.077 附近。任何想法为什么会这样?

My code is:

我的代码是:

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

int main(void)
{
double array[3];

double rand_max = RAND_MAX;

srand((int)time(NULL));

for (int iii = 0; iii < 3; iii++)
    array[iii] = rand() / rand_max;

for (int iii = 0; iii < 3; iii++)
    printf("Element %d is: %lf.\n", iii, array[iii]);

return(0);
}

The output for several runs was as follows:

几次运行的输出如下:

[Session started at 2010-09-11 21:19:07 -0500.]

Element 0 is: 0.076918.

Element 1 is: 0.753664.

Element 2 is: 0.824467.

The Debugger has exited with status 0.
[Session started at 2010-09-11 21:19:12 -0500.]
Element 0 is: 0.076957.
Element 1 is: 0.411353.
Element 2 is: 0.602494.

The Debugger has exited with status 0.

[Session started at 2010-09-11 21:19:16 -0500.]

Element 0 is: 0.076988.

Element 1 is: 0.937504.

Element 2 is: 0.624915.

The Debugger has exited with status 0.

[Session started at 2010-09-11 21:19:24 -0500.]

Element 0 is: 0.077051.

Element 1 is: 0.989806.

Element 2 is: 0.669757.

回答by Alex

try randomize random generator without casting it to in

尝试随机化随机生成器而不将其强制转换为

/* initialize random seed: */
srand ( time(NULL) );


Holly molly... this one is not easy. I remember long time ago in my stochastic models I had this problem. The solution was to use some pragma derective, as far as I remember.

Holly molly……这个不容易。我记得很久以前在我的随机模型中我遇到过这个问题。据我所知,解决方案是使用一些 pragma 指令。

anyways below is solution and it works. Random numbers every time... kind of cheezy though.

反正下面是解决方案,它的工作原理。每次都是随机数……虽然有点俗气。

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

int main(void)
{
    double array[10];

    double rand_max = RAND_MAX;

    srand(time(NULL));

    for (int iii = 0; iii < 9; iii++){
        rand();
        array[iii] = rand() / rand_max;
    }

    for (int iii = 0; iii < 9; iii++){
        printf("Element %d is: %lf.\n", iii, array[iii]);
    }


    return(0);
}

回答by jamesdlin

Typical randimplementations simply aren't very good. You could throw away the first few results, or you could switch to a better pseudo-random number generator. Also see: I need a random number generator.from the comp.lang.c FAQ.

典型的rand实现并不是很好。您可以丢弃前几个结果,或者您可以切换到更好的伪随机数生成器。另请参阅:我需要一个随机数生成器。来自 comp.lang.c 常见问题解答。

回答by tc.

rand() is usually a linear congruential generator. I suspect it's rand-fbsd.c. time() is not a very good seed either, since only the bottom few bits change significantly over a short time.

rand() 通常是一个线性同余生成器。我怀疑它是rand-fbsd.c。time() 也不是一个很好的种子,因为只有底部的几位在短时间内发生了显着变化。

random() is a bit better. Also consider seeding using sranddev() or srandomdev().

random() 好一点。还可以考虑使用 sranddev() 或 srandomdev() 进行播种。