rand() returns same values when called within a single function

前端 未结 5 1976
清酒与你
清酒与你 2020-11-22 15:25

I\'m a C++ newbie and I\'m stumped on this. I need to call this function in my main function three times but each time it gives me the same result, i.e. pull_1, pull_2, pul

5条回答
  •  臣服心动
    2020-11-22 16:07

    The random number generator is reset to an initial state, which is dictated by the seed value, every time you call srand. Time value may be the same between successive calls to time, hence the same seed and the same number generated.

    Call seeding function (srand) only once in your main function before generating random samples.

提交回复
热议问题