C ++中的函数rand [英] Function srand in C++

查看:125
本文介绍了C ++中的函数rand的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

此代码

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

int main ()
{
    printf ("First number: %d\n", rand() % 100);
    srand ( time(NULL) );
    printf ("Random number: %d\n", rand() % 100);
    srand ( 1 );
    printf ("Again the first number: %d\n", rand() %100);

    return 0;
}

具有以下输出:

First number: 41
Random number: 13
Again the first number: 41

还有以下规则:

在相同情况下,两个不同的初始化会指示伪随机数生成器为两种情况下的随后对rand的调用生成相同的结果序列.

我理解这些单词,但我只是不理解该方法本身.为什么它又返回41?是随机的还是必须根据此代码在每种情况下返回相同的结果?

解决方案

如果在没有先调用srand()的情况下调用rand(),则行为就像您以1作为参数调用了srand(). >

此行为在原始C标准中定义.我没有完整的副本,但是The Open Group的POSIX标准是下一个最好的选择,因为它们结合了完整的C标准(带有一些扩展):

http://www.opengroup.org/onlinepubs/000095399/functions/rand.html

srand()函数使用该参数作为新的伪随机数序列的种子,该序列将由对rand()的后续调用返回.如果随后以相同的种子值调用srand(),则应重复伪随机数的序列.如果在对srand()进行任何调用之前调用rand(),则将生成与种子值1首次调用srand()时相同的序列.

对于任何给定种子的rand()调用的实际结果是实现定义的,除了对于任何种子 x 而言,在rand()之后调用 n rand()生成器的种子必须返回相同的结果.因此,在实现上,您在调用srand(1)之后的第一个rand()调用中始终会得到41,并且对于第二个rand()调用将始终获得相同的结果(无论结果如何),其他实现可能会使用不同的结果产生不同结果的算法.

This code

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

int main ()
{
    printf ("First number: %d\n", rand() % 100);
    srand ( time(NULL) );
    printf ("Random number: %d\n", rand() % 100);
    srand ( 1 );
    printf ("Again the first number: %d\n", rand() %100);

    return 0;
}

has the following output:

First number: 41
Random number: 13
Again the first number: 41

There is also the following rule:

Two different initializations with the same seed, instructs the pseudo-random generator to generate the same succession of results for the subsequent calls to rand in both cases.

I understand the words but I just don't understand the method itself. Why did it return 41 again? Is it random or must it return the same result in every case according to this code?

解决方案

If you call rand() without first calling srand(), the behavior is as if you called srand() with 1 as the argument.

This behavior is defined in the original C standard. I don't have a full copy of it handy, but The Open Group's POSIX standards are the next best thing, as they incorporate the full C standard (with some extensions):

http://www.opengroup.org/onlinepubs/000095399/functions/rand.html

The srand() function uses the argument as a seed for a new sequence of pseudo-random numbers to be returned by subsequent calls to rand(). If srand() is then called with the same seed value, the sequence of pseudo-random numbers shall be repeated. If rand() is called before any calls to srand() are made, the same sequence shall be generated as when srand() is first called with a seed value of 1.

The actual result of a rand() call for any given seed is implementation-defined, except that for any seed x, call n to rand() after the seeding of the generator must return the same result. So while on your implementation you will always get 41 on the first rand() call after calling srand(1), and you will always get the same result (whatever it is) for the second rand() call, other implementations may use a different algorithm that produces different results.

这篇关于C ++中的函数rand的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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