0

The man page for rand_r() states:

The value pointed to by the seedp argument of rand_r() provides only a very small amount of state, so this function will be a weak pseudo-random generator.

Could someone explain what this sentence means? How is it that the seed value "provides only a very small amount of state", and how does that weaken the generator?

Before I start using the GNU extension drand48_r() as a better alternative, I want to be sure I am making an informed choice. Perhaps for some use cases rand_r() is sufficient, otherwise there would be no sense in it being available.

EDIT in response to comments:

I wish to better undestand the functions and the way they are explained, preferrably without reference to a particular use case that could constrain the explanation. There are plenty of use cases discussed on the net, but general knowledge about the functions is either lacking or difficult to understand. SO has in the past been great with Q&A introductions, hence my question.

If it helps, I have done a test to compare rand() and rand_r(), producing two random RGB images, where the pixels were generated with:

pxsPtr[i] = (uint8_t) rand() % 255; // rand() version

pxsPtr[i] = (uint8_t) rand_r(&randRseed) % 255; // rand_r() version

I'm surprised by their similarity and now even more eager to obtain answers to my questions.

rand() test

rand_r() test

Theo d'Or
  • 783
  • 1
  • 4
  • 17
  • `seedp` is pointer to unsigned int, so (on a typical system) there are at most 32 bits of state. – Nate Eldredge Jan 17 '20 at 19:36
  • 1
    List some example of your use cases. If they include randomizing to generate broad band randomness, with no bias, then `rand()`, `rand_r()`, etc will not work for you, otherwise they are fine. Here are some related links on the topic: [ref1](https://stackoverflow.com/questions/2775578/biased-random-number-generator), [ref2](https://rosettacode.org/wiki/Unbias_a_random_generator), [ref3](https://zuttobenkyou.wordpress.com/2012/10/18/generating-random-numbers-without-modulo-bias/) – ryyker Jan 17 '20 at 19:46
  • What are you using the generator for? – Jean-Baptiste Yunès Jan 17 '20 at 19:48
  • Neither is a very good random number generator; they're both unsuitable anywhere you need unpredictability. You should clarify what you actually want to get out of this question. – R.. GitHub STOP HELPING ICE Jan 17 '20 at 21:31
  • To all commenters - please see my edit to the question. – Theo d'Or Jan 18 '20 at 14:23
  • `general knowledge about the functions` [rand_r()](https://github.com/lattera/glibc/blob/master/stdlib/rand_r.c#L27) is really simple function. – KamilCuk Jan 18 '20 at 14:33

0 Answers0