[Tfug] /dev/random
Charles R Kiss
charles at kissbrothers.com
Sat Jun 11 18:38:47 MST 2005
I stole the following code:
#include <iostream>
#include <fstream>
#include <cstdlib>
using namespace std;
//const char *rand_device = "/dev/urandom";
const char *rand_device = "/dev/random";
int
main() {
int getuid();
cout << "uid " << getuid() << endl;
std::ifstream random(rand_device);
if (!random) {
perror(rand_device);
return 1;
}
unsigned int seed;
random.read((char*)&seed, sizeof(seed));
cout << "seed: " << seed << endl;
return 0;
}
from here:
http://groups-beta.google.com/group/comp.lang.c++.moderated/browse_thread/thread/3585188f84021b65
but when compiling: charles at house:~/game$ g++ t.C -o t
I get the following error:
t.C: In function `int main()':
t.C:18: implicit declaration of function `int perror(...)'
Why? I tried adding the <stdio> directive; but that didn't help. But
I did find this in /usr/include/stio.h
/* Print a message describing the meaning of the value of errno. */
extern void perror (__const char *__s) __THROW;
The above code when simplified to:
#include <iostream>
#include <fstream>
#include <cstdlib>
using namespace std;
//const char *rand_device = "/dev/urandom";
const char *rand_device = "/dev/random";
int
main() {
std::ifstream random(rand_device);
unsigned int seed;
random.read((char*)&seed, sizeof(seed));
cout << "seed: " << seed << endl;
return 0;
}
generates random numbers quickly, but I guess I'm feeling like I'm
missing a check on the entropy pool (something I don't yet understand);
but I haven't figured that out yet, I'm just guessing. How fast and
for how long can I generate these numbers; which are rather large.
Is there a way of limiting the random numbers output to three or four
numbers numbers without using the modulus function? Say, reading
straight off a 2-bit byte random stream, or a 3-bit byte stream, or an
n-bit byte stream?
For now, I just need fast zeros and ones randomly distributed; but most
highly random. Basically, super-fast, super-random, but only 0's and
1's; then, later, I guess, not-so-fast, but just as random, values
betwen 0 and 4 [is this possible, or is the randomness a function of the
byte size??]; ie. controlling the size of the byte, then dumping the
values into a array. Just for fun, obviously I have much more reading
to do.
Also, is controlling the entropy pool, as in BSD's rndcontrol function,
possible? Or do I just throw the processor with keyboard and mouse down
the stairs while it's computing?
Thanks in advance,
Charles
ps. I joined the dclug here; but it just won't be the same :( .
Especially the outdoor air temperature, which should normally feel
below 127F.
More information about the tfug
mailing list