c++ - 循环中无法生成不同的随机数
怪我咯
怪我咯 2017-04-17 15:17:23
0
2
392
for(i=0;i<h;i++)
{    
srand(time(0));
    b[i]=rand()%3;
        }
    在这个循环中b[i]不变 怎样才能让它变化?
怪我咯
怪我咯

走同样的路,发现不同的人生

reply all(2)
Peter_Zhu

b[i] must not change in the loop, because you will initialize PRNG every time you loop.
srand(time(0)); should be placed outside the loop.

阿神
srand(time(0));
for(i=0;i<h;i++)
{    
    b[i]=rand()%3;
}
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template