Advertisement

rand() help!

Started by November 04, 2002 10:06 AM
10 comments, last by Le0 22 years ago
It''s more like learning Dutch while standing in the middle of Berlin. (FYI: Dutch and German are related languages with many similarites, but also many differences). OpenGL is an API based firmly in C. Usable in C++, but still quite unlike the actual object oriented C++ stuff most books will teach you.

Kippesoep
As a piece of advice, you should know that all PRNG algorithms that I am aware of will eventually cycle into a repeated pattern. If you are generating a very large number of psuedorandom numbers (ten thousand or more), it may be wise to periodcally reseed the RNG. You can wrap the whole functionality into a single function like this:

      #include <stdlib.h> /* in C++, use <cstdlib> instead */ #include <time.h>   /* in C++, use <ctime> instead */#define INTERVAL 1024 /* modify this as needed to optimize results */double random(){ static int count = 0;  if (0 == count)   srand(time(0)); count++; count %= INTERVAL; /*if count==INTERVAL, then set count to 0*/ return rand();}      


You should set the INTERVAL constant to one that seem to get the best results under your system.

You should also know that many (but not all) newer PC motherboards have hardware random number generators. Accessing them, however, depends on the motherboard and the OS you are using. In Linux, IIUC, this functionality is accessed with the /dev/rand pseudodevice (though not all RNGs are supported); I don't know offhand how it would be accessed in Windows.

Trouble rather the tiger in his lair than the Scholar among his books

[edited by - Schol-R-LEA on November 6, 2002 4:41:08 PM]
Trouble rather the tiger in his lair than the Scholar among his books

This topic is closed to new replies.

Advertisement