Hi, im fairly new to C++ and would like help with using random number generators, i have heard of them but cant seem to find there code structure.
Any help is greatly appreciated :D

http://www.agner.org/random/randomc.zip

download that zip of .cpp files

File list

randomc.h
C++ header file containing class definitions.
You must #include this in all C++ files that use this library.

mersenne.cpp
Random number generator of type Mersenne twister.

ranrotb.cpp
Random number generator of type RANROT-B.

ranrotw.cpp
Random number generator of type RANROT-W.

mother.cpp
Random number generator of type Mother-of-all (multiply with carry).

rancombi.cpp
Template class for combining any two of these random number generators.

ex-ran.cpp
Example showing how to use these random number generators.

That should help alot in understanding the generators. Be aware that not all generators work with all platforms as c++ is multiplatform the generators will behave differently depending on those conditions.

If you do a google on the generator names you should find which are platform specific.

RANROT-W will work for windows as a starter though. Have fun learning ! :D

Your C++ compiler comes with a random number generator, rand(). There's a tutorial on the site here:

[thread]1769[/thread]

If you're producing a serious application you'll want something a little more sophisticated but for many purposes it's adequate.

Well producing a random number is very simple. Before you use the random number:
1. include time.h.
2. just put randomize() at the beginning of code.
3. Then use random() or if you want a range of random number, for e.g out of 4 to 20 you want one, declare
random(20-4+1)+4;
random(n) returns numbers from 0 to n-1, hence 1 was added, so as general:
random(upper limit-lower limit +1) + lower;
Since random returns int, you have to store it in int variable.

I hope that much would make it clear. And I know that you really do not need too complicated random functions at this moment, for if it is,lol, dont ask me, i dont know. These are enough.

OP posted Jul 18th, 2003 ... :icon_wink:

Hi Matthew,

below is a piece of code showing a pseudo random generator based on the linear congruential method by Lehmer to generating uniformly distributed random numbers over 0 ... s - 1. Random generators implemented in today programming languages are usually based on this method. Sure, they might be better and faster then mine, but lehmer is a complete random generator what also has rather good statistical properties. Its period is 2147483646.

unsigned long int lehmer(long int s)
//  linear congruential pseudo random number generator based on D. Lehmer
{  static unsigned long long a = 2007, b = 4194301, c = 2147483647, z = b;
    if ( s < 0 ) {s = -s; a = s;}
    z = (a + b * z) % c;
    return z % s;
}

If you call lehmer with s > 0, the random number is drawn from interval 0...s-1. Every time you start it with same s it produces the same sequence of random numbers, what is favorable when doing physical experiments. For game programming it s better to start it with s < 0 to initialize lehmer to produce various sequences. So s < 0 is kind of random seed.

Hope this will help you to understand how random generators work.

Theory of random generators can be found in D. Knuth: The Art of Computer Programming. One of the best set of random generators ever written is Mersenne Twister MTrand.

krs,
tesu

:'( oops sorry, now i got it: This thread was posted in 2003 ! Well, never mind ...

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.