简体   繁体   English

为什么每次编译并运行rand()都会得到相同的结果?

[英]Why do I get the same result with rand() every time I compile and run?

Whenever I run this code, I get a same result. 每当我运行此代码时,我都会得到相同的结果。

Program 程序

#include<stdlib.h>

int main(int agrc, const char *argv[]) {
 int i = rand();
 printf("%d\n",i);
 for(i=0;i<10;i++) {
  printf("%d\n",rand());
 }
}

Result: 结果:

41
18467
6334
26500
19169
15724
11478
29358
26962
24464
5705

I ran this on mingw . 我在mingw上运行它。 Actually I am learning Objective-C 其实我正在学习Objective-C

Please help me. 请帮我。

You need to seed the rand function with a unique number before it can be used. 您必须在使用唯一编号之前为rand函数添加种子。 The easiest method is to use time() 最简单的方法是使用time()

For example 例如

srand(time(NULL));
rand();//now returns a random number

The reason is that the random numbers provided by rand() (or any other algorithm based function) aren't random. 原因是rand()提供的随机数(或任何其他基于算法的函数)不是随机的。 The rand function just takes its current numerical state, applies a transformation, saves the result of the transformation as the new state and returns the new state. rand函数仅采用其当前数值状态,应用转换,将转换结果另存为新状态并返回新状态。

So to get rand to return different pseudo random numbers, you first have to set the state of rand() to something unique. 因此,要让rand返回不同的伪随机数,您首先必须将rand()的状态设置为唯一。

You want to initialize the PRNG. 您要初始化PRNG。

Initialize it once (typically inside main() ) with a call to the srand() function. 调用一次 (通常在main()内部),并调用srand()函数。

If you do not initialize the PRNG, the default is to have it initialized with the value 1 . 如果不初始化PRNG,则默认为使用值1对其进行初始化。 Of course initializing it with some other constant value will not give you different pseudo random numbers for different runs of the program. 当然,使用其他一些常量初始化它不会为程序的不同运行提供不同的伪随机数。

srand(1); /* same as default */
srand(42); /* no gain, compared to the line above */

You need to initialize with a value that changes with each run of the program. 您需要使用一个随程序每次运行而变化的值进行初始化。 The value returned from the time() function is the value most often used. time()函数返回的值是最常用的值。

srand(time(NULL)); /* different pseudo random numbers almost every run */

The problem with time(NULL) is that it returns the same value at the same second. time(NULL)的问题在于它在同一秒返回相同的值。 So, if you call your program twice at 11:35:17 of the same day you will get the same pseudo random numbers. 因此,如果您在同一天的11:35:17两次调用程序,则将获得相同的伪随机数。

Just to add to Yacoby's answer - I was slightly surprised that it didn't default to a time-based seed, so I looked up the man page : 只是为了增加Yacoby的答案-我对它没有默认为基于时间的种子感到有些惊讶,所以我查阅了手册页

If no seed value is provided, the rand() function is automatically seeded with a value of 1. 如果没有提供种子值,则rand()函数将自动以1值作为种子。

So if you change your code to use seed(1) you should still see the same output - but seed(time()) will make it change each time. 因此,如果将代码更改为使用seed(1)您仍然应该看到相同的输出-但是seed(time())会使它每次都更改。

The output from rand is pseudo -random, which means that it looks effectively random, but is computed the same way each time, starting from a special value called the seed. rand的输出是随机的,这意味着它看起来实际上是随机的,但是每次都从一个称为种子的特殊值开始,以相同的方式进行计算。 With the same seed value, you get the same sequence of random numbers. 使用相同的种子值,您将获得相同的随机数序列。

To set a different seed, use the standard C function void srand(unsigned int) once in your code before you start generating random numbers. 要设置其他种子,请在开始生成随机数之前在代码中使用一次标准C函数void srand(unsigned int) One common way of getting a different sequence of random numbers each time you run the program is to base the seed on the clock time. 每次运行程序时,获取不同随机数序列的一种常见方法是将种子基于时钟时间。 Eg srand(clock()) 例如srand(clock())

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

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