I want to know how rand() works (even when I don't provide any seed how it produces PRNs?) thanks!
- Anybody can ask a question
- Anybody can answer
- The best answers are voted up and rise to the top
closed as off topic by fgrieu, Paŭlo Ebermann Mar 21 '13 at 21:09
Questions on Cryptography Stack Exchange are expected to relate to cryptography within the scope defined by the community. Consider editing the question or leaving comments for improvement if you believe the question can be reworded to fit within the scope. Read more about reopening questions here.If this question can be reworded to fit the rules in the help center, please edit the question.
Well, successive calls rand() just produce numbers that "look random".
Now, rand() doesn't take a seed; that means that everytime the program runs, calls to rand() will generate the exact same sequence of numbers. This is a deliberate design decision; that means that the program behavior is reproducible (which can be important if you're debugging). If you don't want this behavior, well, that's why srand() is provided.
As for what "looks random" means, well, it essentially means "if you eyeball the output, no obvious pattern jumps out at you".
When working with cryptography (you did ask in the crypto stack exchange), we don't have much use for rand(); even if you feed in a seed via srand(), that seed is generally too small to be useful, and even if the rand() output isn't obviously patterned, crypto has much higher criteria for randomness.
On the other hand, other uses need not have such high standards. I believe rand() may be useful within some randomized algorithms; just not cryptographical ones.
You can see in stdlib.c (I found it here) that rand() is defined as:
Of couse this may vary from disto to distro and version to version. You should find your local stdlib.c file (or the one that corresponds to your distro) to see how exactly it's implemented. srand() merely changes holdrand:
so as @poncho said it merely garbles the output. No true randomness whatsoever.