0
$\begingroup$

I want to know how rand() works (even when I don't provide any seed how it produces PRNs?) thanks!

$\endgroup$
  • 1
    $\begingroup$ Given the typical implementation of rand(), you're using a quite generous definition of "works". IMO it does not work, even for non security related stuff. $\endgroup$ – CodesInChaos Mar 20 '13 at 17:47
  • 1
    $\begingroup$ The rand() function of C is not a cryptographic pseudo-random generator, and as such off-topic here. Therefore I'm closing this question. $\endgroup$ – Paŭlo Ebermann Mar 21 '13 at 21:11
3
$\begingroup$

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.

| improve this answer | |
$\endgroup$
  • 1
    $\begingroup$ Even when eyeballing the plot of rand() it often exhibits total failure $\endgroup$ – CodesInChaos Mar 20 '13 at 17:48
1
$\begingroup$

You can see in stdlib.c (I found it here) that rand() is defined as:

static long holdrand = 1L;
...
int rand() {
  return (((holdrand = holdrand * 214013L + 2531011L) >> 16) & 0x7fff);
}

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:

void srand(unsigned int seed) {
   holdrand = (long) seed;
}

so as @poncho said it merely garbles the output. No true randomness whatsoever.

| improve this answer | |
$\endgroup$
  • $\begingroup$ That implementation is just one possibility; in fact, the C standard provides a different sample implementation (similar, but with a different multiplier and constant). Different C compilers/libraries are perfectly free to use something else (and likely will). $\endgroup$ – poncho Mar 20 '13 at 19:04
  • $\begingroup$ Yes that's very likely. I posted the source to show there's no elaborate algorithms behind rand(), just vile trickery and deception. $\endgroup$ – rath Mar 20 '13 at 19:08
  • $\begingroup$ OMG, that's exactly how rand() works on Windows systems!!!! Thank you so much $\endgroup$ – Leo Leontev Nov 10 '19 at 13:48

Not the answer you're looking for? Browse other questions tagged or ask your own question.