rand() generating the same number – even with srand(time(NULL)) in my main!

后端 未结 7 652
执念已碎
执念已碎 2020-11-28 15:49

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 a

7条回答
  •  日久生厌
    2020-11-28 16:16

    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.

提交回复
热议问题