简体   繁体   English

兰德功能改进

[英]Rand function improvement

My code initializes a pointer to a random name. 我的代码初始化一个指向随机名称的指针。 The random name is randomly selected by using the rand function. 随机名称是使用rand函数随机选择的。

I have noticed that the rand function does not quite do what I want it to do. 我注意到rand函数不能完全按照我的意愿去做。 In the following code, it initializes all the pointers with the same name! 在下面的代码中,它初始化所有具有相同名称的指针! I suspect this has do to with the rand function basing its selection on the time function. 我怀疑这与rand函数有关,它的选择基于时间函数。 If it initializes all the pointers at once, it will produce the same random number!? 如果它一次初始化所有指针,它将产生相同的随机数!? I'm sure it takes time for the time stamp to produce another number right? 我确定时间戳需要时间来产生另一个数字吗? I can fix the code by running the initializations in a loop but I would like to know another way to fix as sometimes it takes a few seconds to work through the loop. 我可以通过在循环中运行初始化来修复代码,但我想知道另一种修复方法,因为有时需要几秒钟来完成循环。

For reference: 以供参考:

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

int  random_number(int, int);
const char *random_name();
//----------------------------------------------------------------
int main(void)
{
    const char * kg = NULL;
    const char * ke = NULL;
    const char * dg = NULL;
    const char * de = NULL;

    kg = random_name();
    ke = random_name();
    dg = random_name();
    de = random_name();

    printf("kg=%s\nke=%s\ndg=%s\nde=%s\n", kg, ke, dg, de);

    return 0;
}
//----------------------------------------------------------------
const char *random_name(int x)
{
    const char *names[7] =
    {"Bob", "Billy", "Buck", "Bobby", "Bill", "Billy Bob", "Bobi"};

    int roll = random_number(0,7);
    return names[roll];
}
//----------------------------------------------------------------
int random_number(int min, int max)
{
    int roll;
    int maximum = max - min;
    srand(time(NULL));
    roll = (rand() % maximum) + min;
    return roll;
}

The rand() function is a pseudorandom number generator that uses the previous generation to calculate the next value. rand()函数是一个伪随机数生成器,它使用上一代来计算下一个值。 By seeding with srand() you are setting the starting value and if you do multiple times you effectively 'reset' the random number sequence. 通过使用srand()播种,您将设置起始值,如果您多次执行,则可以有效地“重置”随机数序列。 Seed the random number generator once (using srand() ) one at the start of your program instead of before each call to rand() . 在程序开始时将随机数生成器播种一次(使用srand() ),而不是在每次调用rand()之前播种。

Move srand(time(NULL)) into your main method. srand(time(NULL))到main方法中。 That seeds the random number generator at the program's entry point, allowing for a different random number with an even distribution from the rest to be retrieved each time rand() is called. 在程序的入口点播种随机数生成器,允许在每次调用rand()时检索其余的随机数,其中包含其余的偶数分布。

I believe the issue lies in the fact that you seed each call to the PRNG with a new value on each run. 我认为问题在于,每次运行都会为PRNG的每次调用添加一个新值。 See what srand() does here . 看看srand()在这里做了什么。 The reason you would want to avoid seeding the PRNG each call is because then you cannot guarantee that the value generated on the next call to rand() is evenly distributed. 您希望避免为每次调用播种PRNG的原因是因为您无法保证在下次调用rand()生成的值均匀分布。 What you're most likely looking for is that even distribution of values, and using srand() in this way will yield unexpected results. 你最有可能寻找的是,均匀分布值,并以这种方式使用srand()将产生意想不到的结果。 This answer will provide more details on why you should only call srand() once. 这个答案将提供更多详细信息,说明为什么你应该只调用一次srand()

Also bear in mind that similar rules would apply to any PRNG in any language, and not just C. 还要记住,类似的规则适用于任何语言的任何PRNG,而不仅仅是C.

Your program runs very quickly, so time returns the same value each time it is called. 您的程序运行速度非常快,因此每次调用time返回相同的值。 Since the value passed to srand (the s stands for "seed") determines what sequence rand will produce, it produces the same number (the initial number or an otherwise-random sequence) each time within the same program run, although the number should vary when you run the program again a second later. 由于传递给srand的值( s代表“种子”)决定rand将产生什么序列,因此每次在同一程序运行中产生相同的数字(初始数字或其他随机序列),尽管数字应该是一段时间后再次运行程序时会有所不同。

The solution, as others have mentioned, is to call srand at the beginning of main instead. 正如其他人所提到的,解决方案是在main的开头调用srand

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM