rand()产生相同的数 - 即使与srand(时间(NULL))在我的主! [英] rand() generating the same number – even with srand(time(NULL)) in my main!

查看:322
本文介绍了rand()产生相同的数 - 即使与srand(时间(NULL))在我的主!的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以,我想创建一个随机向量(想几何,不是一个可扩展的数组),每次我调用我的随机向量函数,我得到相同的x值,虽然y和z是不同的。

  int main(){
srand((unsigned)time(NULL));
矢量< double>一个;
a.randvec();
cout<< a<< endl;
return 0;
}

pre> // random Vector
template< class T>
void Vector< T> :: randvec()
{
const int min = -10,max = 10;
int randx,randy,randz;

const int bucket_size = RAND_MAX /(max-min);

do randx =(rand()/ bucket_size)+ min;
while(randx <= min&& randx> = max);
x = randx;

do randy =(rand()/ bucket_size)+ min;
while(randy< = min&& randy> = max);
y = randy;

do randz =(rand()/ bucket_size)+ min;
while(randz< = min&& randz> = max);
z = randz;
}

由于某种原因,randx会一直返回8,完全遵循(伪)随机性。然而,如果我把调用定义,说,randy之前randx,randy将总是返回8.



为什么我的第一个随机数总是8?我的播种不正确?

解决方案

问题是随机数生成器正在播种与非常接近的值 - 每次运行程序只改变time()的返回值一个小量 - 也许1秒,甚至可能没有!相当差的标准随机数发生器然后使用这些相似的种子值来生成明显相同的初始随机数。基本上,你需要一个比rand()更好的初始种子生成器和一个比rand()更好的随机数生成器。



使用的实际循环算法是我想从Accelerated C ++并且旨在产生比在使用mod运算符时更好的在所需范围上的数字分布。但它不能补偿总是(有效)给予相同的种子。


So, I'm trying to create a random vector (think geometry, not an expandable array), and every time I call my random vector function I get the same x value, though y and z are different.

int main () {
    srand ( (unsigned)time(NULL));
    Vector<double> a;
    a.randvec();
    cout << a << endl;
    return 0;
}

using the function

//random Vector
template <class T>
void Vector<T>::randvec()
{
    const int min=-10, max=10;
    int randx, randy, randz;

    const int bucket_size = RAND_MAX/(max-min);

    do randx = (rand()/bucket_size)+min;
    while (randx <= min && randx >= max);
    x = randx;

    do randy = (rand()/bucket_size)+min;
    while (randy <= min && randy >= max);
    y = randy;

    do randz = (rand()/bucket_size)+min;
    while (randz <= min && randz >= max);
    z = randz;
}

For some reason, randx will consistently return 8, whereas the other numbers seem to be following the (pseudo) randomness perfectly. However, if I put the call to define, say, randy before randx, randy will always return 8.

Why is my first random number always 8? Am I seeding incorrectly?

解决方案

The issue is that the random number generator is being seeded with a values that are very close together - each run of the program only changes the return value of time() by a small amount - maybe 1 second, maybe even none! The rather poor standard random number generator then uses these similar seed values to generate apparently identical initial random numbers. Basically, you need a better initial seed generator than time() and a better random number generator than rand().

The actual looping algorithm used is I think lifted from Accelerated C++ and is intended to produce a better spread of numbers over the required range than say using the mod operator would. But it can't compensate for always being (effectively) given the same seed.

这篇关于rand()产生相同的数 - 即使与srand(时间(NULL))在我的主!的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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