[Tfug] /dev/random

Bowie J. Poag bpoag at comcast.net
Sat Jun 11 19:48:25 MST 2005


Hi Charles,

/dev/random in most *nix'es typically only maintains an 8K entropy pool, 
IIRC.  For small amounts of entropy, /dev/random is fine. Otherwise, go 
with urandom instead.

You might be getting the perror() error (heh) because stderr isn't 
included....Dunno, though.. Never seen that one before.  The only other 
way I can think of to get arbitrary lengths without taking the modulus 
of a given value is to take bitwise shifts on it. Throwing the keyboard 
and mouse down the stairs will actually keep the entropy pool filled.

What kind of platform are you running this on? If it's AIX, you need to 
tread lightly. It's very easy to piss all over yourself by having a 
process sit and hang waiting for something to come out of /dev/random. 
AIX and /dev/random don't get along well, from my experience.

Cheers,
Bowie

Charles R Kiss wrote:

> 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.
> _______________________________________________
> tfug mailing list
> tfug at tfug.org
> http://www.tfug.org/mailman/listinfo/tfug
>



More information about the tfug mailing list