为什么我的 srand(time(NULL)) 函数每次在 c 中生成相同的数字? [英] Why does my srand(time(NULL)) function generate the same number every time in c?

查看:80
本文介绍了为什么我的 srand(time(NULL)) 函数每次在 c 中生成相同的数字?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以我创建了一个程序,它会调用一个函数并返回 0 或 1(0 表示尾部,1 表示正面),然后用它来打印 100 次翻转的结果.

So I was creating a program that would call a function and return 0 or 1 (0 meaning tails and 1 meaning heads) and then use that to print the outcome of 100 flips.

认为我可以使用 srand(time(NULL)) 为 rand() 播种不断变化的种子,这似乎很简单.这是我的第一次破解.

It seemed simple enough thinking I could use srand(time(NULL)) to seed rand() with constantly varying seeds. Here was my first crack.

#include <stdio.h>
#include <stdlib.h>
int flip();


int main(void) {

    int heads = 0;
    int tails = 0;

    for (short int count = 1; count <= 100; ++count) {

        int number = flip();

        if (number == 0) {
            printf("%s", "Tails");
            ++tails;
        }
        else if (number == 1) {
            printf_s("%s", "Heads");
            ++heads;
        }

    }//end for
    printf_s("\n%d Tails\n", tails);
    printf_s("%d Heads", heads);
}//end main

int flip(void) {

    srand(time(NULL));
    int number = (int)rand();
    printf("%d", number%2);
    return number%2;
}//end flip

我会运行程序,我的 rand() 值将始终是在 for 语句的每次迭代中重复的五位整数(即 15367、15745 或 15943).

I would run the program and my rand() value would always be a five digit integer repeated in each iteration of the for statement (i.e 15367, 15745, or 15943).

我一直在胡闹,直到我发现将 srand(time(NULL)) 更改为 srand(time(NULL)*time(NULL)/rand()) 才奏效.

I messed around until I discovered changing srand(time(NULL)) to srand(time(NULL)*time(NULL)/rand()) did the trick.

我唯一的想法是每次迭代之间的时间太小以至于 srand() 函数的 time(NULL) 部分没有变化到足以提供不同的种子值.

My only thought is that the time between each for iteration is so small the the time(NULL) part of the srand() function doesn't change enough to feed a different seed value.

我也尝试过 srand(time(NULL)/rand()),但是,每次运行程序(20 次以上)时,都会产生相同的结果(52 个正面 48 个反面);然而,rand() 的值都彼此不同.

I also tried srand(time(NULL)/rand()), however, this produced the same result (52 heads 48 tails) every time I ran the program (20+times); however, the rand() values were all different from each other.

我不知道为什么会发生这些事情,或者为什么最终的 srand(time(NULL)*time(NULL)/rand()) 函数起作用,如果有人能解释一下,我会很高兴的!

I do not know why these things happened, or why the final srand(time(NULL)*time(NULL)/rand()) function worked, and I would love it if someone could explain!

推荐答案

原因是 time(NULL) 每秒只改变一次!这意味着,您使用 相同 种子为随机数生成器播种 100 次.更好的方法是在流程开始时只为 RNG 设置一次种子(在 main() 的开头,然后你应该得到不同的值.

The reason is, that time(NULL) changes only once per second! This means, that you seed the random number generator 100 times with the same seed. A better way is to seed the RNG only once at start of the process (at the head of main(), then you should get different values.

如果您启动程序的频率超过一秒一次,您也可以使用

If you start your program more often than once a second, you could also seed it with

srand(time(NULL)+getpid());

或类似的.

这篇关于为什么我的 srand(time(NULL)) 函数每次在 c 中生成相同的数字?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆