Rand Implementation

前端 未结 3 1581
南方客
南方客 2020-11-27 06:57

I would like to go through how rand() and srand() functions are implemented and would like to tweak the code to modify it to my requirements. Where can i find the source cod

3条回答
  •  醉梦人生
    2020-11-27 07:09

    The glibc one (used by gcc) is the simple formula:

    x = 1103515245 * x + 12345
    

    wrapping around at 232, as shown here. You can just set x as the seed then keep calling a function to evaluate that expression (and update the seed).

    But you should be aware the linear congruential generators like this are considered adequate but not ideal.

    While the only ideal random number generator would be perfectly random, the Mersenne Twister probably comes closer.

提交回复
热议问题