How does srand relate to rand function?

后端 未结 4 1956
你的背包
你的背包 2020-12-02 17:39

I understand that rand() function generates the same number(s) each you run it if you don\'t change the seed number. That\'s where srand() comes in. Time is always changing

4条回答
  •  孤街浪徒
    2020-12-02 18:30

    rand gives you a pseudo random sequence of numbers.

    This number is generated by an algorithm that returns a sequence of apparently non-related numbers each time it is called. This algorithm uses a seed to generate the series, which should be initialized to some distinctive value using function srand.

    srand on each call sets the pointer to some location in the list which you are getting in. If you don't call it on each attempt or give it a fix seed it will give you the same sequence. So many suggest giving the current second as seed. But if you try running your code in the same second twice, it will give you same sequence.

    For every different seed value used in a call to srand, the pseudo-random number generator can be expected to generate a different succession of results in the subsequent calls to rand for further explanation

提交回复
热议问题