[英]rand() produces the same result on each function call (with srand(time(0))
I have a member function of a class that is supposed to generate a random number in a range.我有一个类的成员函数,它应该在一个范围内生成一个随机数。 To do so, I am using the rand() function.为此,我使用了 rand() 函数。 The function generates a random number like this:该函数生成一个随机数,如下所示:
unsigned seed;
seed = time(0);
srand(seed);
std::cout << "Random Number: "<< rand() << std::endl;
The function is called on two different objects.该函数在两个不同的对象上调用。 The result is:结果是:
Random Number: 1321638448
Random Number: 1321638448
This is consistent every-time I call it.每次我调用它时,这都是一致的。 What am i doing wrong?我究竟做错了什么?
(Converting my comment to an answer). (将我的评论转换为答案)。
For most applications, you'll only really want to seed rand
once in the course of running a program.对于大多数应用程序,您实际上只想在运行程序的过程中为rand
一次种子。 Seeding it multiple times requires you to get different random seeds, and it's easy to mess that up.多次播种需要您获得不同的随机种子,这很容易搞砸。
In your case, the time
function usually returns something with resolution on the level of seconds (though this isn't actually required by the standard ).在您的情况下, time
函数通常返回具有秒级分辨率的内容(尽管标准实际上并不要求这样做)。 As a result, if you call time
twice within the same second, you might get back the same value.因此,如果您在同一秒内两次调用time
,您可能会返回相同的值。 That would explain why you're getting duplicate values: you're seeding the randomizer with the same value twice and then immediately querying it for a random number.这将解释为什么你会得到重复的值:你用相同的值两次播种随机发生器,然后立即查询它的随机数。
The best solution to this is to just seed the randomizer once.对此的最佳解决方案是只为随机化器设置一次种子。 Typically, you'd do that in main
.通常,您会在main
执行此操作。
If you really do want to seed the randomizer multiple times, make sure that you're doing so using a seed that is going to be pretty much random.如果您确实想多次为随机化器设置种子,请确保您使用的种子几乎是随机的。 Otherwise, you risk something like this happening.否则,您将面临发生此类事件的风险。
Pseudorandom number generators basically have to pass a set of statistical tests to make sure they're "random enough" as a set of numbers.伪随机数生成器基本上必须通过一组统计测试,以确保它们作为一组数字“足够随机”。 But of course, it's not actually random.但当然,它实际上并不是随机的。 Calling srand(seed)
with some seed
basically generates a set of numbers which, if passed through those tests, will seem "random enough".用一些seed
调用srand(seed)
基本上会生成一组数字,如果通过这些测试,这些数字看起来“足够随机”。
By calling srand(seed)
with the same seed
multiple times, you're effectively generating the same set over and over again and getting the first value in it.通过使用相同的seed
多次调用srand(seed)
,您可以有效地一遍又一遍地生成相同的集合并获得其中的第一个值。
You call srand(seed)
ONCE, and then you call rand()
to get the next values in the random number set.您调用srand(seed)
一次,然后调用rand()
以获取随机数集中的下一个值。 Or you need to call srand(seed)
with a different (random) seed each time.或者您需要每次使用不同的(随机)种子调用srand(seed)
。
If you're on linux, you can also use /dev/urandom
to get a random number- the kernel has been taking signal/noise from the environment to generate "entropy" for it, supposedly making it even better than an algorithm psuedorandom number generator.如果您使用的是 linux,您还可以使用/dev/urandom
来获取随机数 - 内核一直在从环境中获取信号/噪声来为其生成“熵”,据说它甚至比算法伪随机数更好发电机。
srand
function should be called only once in program(most cases, not all cases). srand
函数应该在程序中只调用一次(大多数情况下,不是所有情况)。 If you want reseed, you should use different seed number.如果你想重新播种,你应该使用不同的种子号。 Because rand()
function is pseudo-random number generator.因为rand()
函数是伪随机数生成器。 In other words, rand()
gives you a calculated number.换句话说, rand()
给你一个计算出的数字。
You can use much for powerful random number generating library after C++11.在 C++11 之后,你可以使用很多强大的随机数生成库。 See: http://en.cppreference.com/w/cpp/numeric/random请参阅: http : //en.cppreference.com/w/cpp/numeric/random
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.