Click to See Complete Forum and Search --> : how can I make random() trully random?
saturn-vk
09-11-2004, 08:06 PM
right now, using:
random() % max
it creates the exact same sequence of numbers whenever the program is started.
I can't create a random seed, because I have to use random to create it, but it will just create the same number when the program start.
EDIT: oh yeah, that's C.
tecknophreak
09-11-2004, 10:09 PM
Use one or combination of time and gettimeofday, time returns the seconds since 1970 and gettimeofday returns the time of day with microsecond resolution(for more info, just run man on each). Use that as your seed and each time you start up your prog, you'll have better random numbers.
However, this does not make "random() trully random", why, because it's a prn(pseudo random number) sequence and if you let it go long enough without reseeding, you'll see a pattern emerge, it will take a while to get there though.
bwkaz
09-11-2004, 10:33 PM
int randoms[256];
int fd;
fd = open("/dev/random", O_RDONLY);
read(fd, randoms, sizeof(randoms));
close(fd);
/* Careful with that sizeof() -- it won't work right unless its operand is an array.
If you want to use it with a pointer, you need to know the number of bytes
pointed to, but sizeof() can't tell you that. */
/* At this point, randoms[] is filled with true random numbers. It might take
a while to get to this point, though (the kernel might block while creating
those numbers). To use them, iterate through randoms[] and treat that as your
sequence. */
saturn-vk
09-12-2004, 05:11 AM
Originally posted by tecknophreak
Use one or combination of time and gettimeofday, time returns the seconds since 1970 and gettimeofday returns the time of day with microsecond resolution(for more info, just run man on each). Use that as your seed and each time you start up your prog, you'll have better random numbers.
However, this does not make "random() trully random", why, because it's a prn(pseudo random number) sequence and if you let it go long enough without reseeding, you'll see a pattern emerge, it will take a while to get there though.
are there any man pages for these functions? in what header are they, time.h?
nm, they were in the 2nd section
X_console
09-12-2004, 09:39 PM
Try man srand. rand() and srand() are both defined in stdlib.h.
Fryguy8
09-12-2004, 10:57 PM
bwkaz, while those are good random numbers, they aren't truly random. A computer is completely incapable of generating true randomness.
Originally posted by bwkaz
int randoms[256];
int fd;
fd = open("/dev/random", O_RDONLY);
read(fd, randoms, sizeof(randoms));
close(fd);
/* Careful with that sizeof() -- it won't work right unless its operand is an array.
If you want to use it with a pointer, you need to know the number of bytes
pointed to, but sizeof() can't tell you that. */
/* At this point, randoms[] is filled with true random numbers. It might take
a while to get to this point, though (the kernel might block while creating
those numbers). To use them, iterate through randoms[] and treat that as your
sequence. */
bwkaz
09-13-2004, 06:37 PM
Originally posted by Fryguy8
bwkaz, while those are good random numbers, they aren't truly random. A computer is completely incapable of generating true randomness. A computer is incapable of generating randomness, yes.
However, a computer is not incapable of seeing and collecting randomness generated by other sources. This is what /dev/random gives you access to.
The kernel keeps a pool of entropy inside itself. Various kernel drivers stuff numbers into that pool every once in a while (e.g. the network driver stuffs the time between the current network packet receipt and the previous one, the keyboard driver stuffs the time between two keypresses, the mouse driver stuffs time between mouse events, and the hardware random-number chip driver stuffs random numbers obtained from the hardware chip, which are also truly random). These numbers are completely random; there's no way to predict or reproduce them, and there's no way to generate the next one in the sequence if you know any one of them.
The kernel entropy pool uses some more math to mix them even further (as they're being added to the pool), and then you can read a sequence of random bytes by read()ing from /dev/random. It also keeps an estimate of the amount of entropy available in the pool, and will suspend reads through /dev/random if that value falls too low. However, if you're reading /dev/urandom, your read continues (and you get worse random numbers, but generally not all that much worse).
The numbers you get from /dev/random are NOT deterministic, are NOT repeatable, and are NOT usually predictable. They are therefore extremely high quality. I would feel comfortable using them to generate ssh keys, for instance, because they are high quality, cryptographically secure, random numbers (ssh-keygen, for instance, almost always uses /dev/random). I would also feel perfectly safe using them to generate passwords if needed.
A note on the hardware RNG chip -- the numbers obtained from it are truly random also, because they're based on algorithms that take into account e.g. the oscillations of individual atoms when those atoms are heated by the rest of the system, or by current passing through them. Some highly advanced chips probably take radio noise into account also (though I wouldn't put these in a PC; I think they'd be too expensive).