| | |
Random number generator's
Please support our C++ advertiser: Intel Parallel Studio Home
![]() |
•
•
Join Date: Jul 2003
Posts: 17
Reputation:
Solved Threads: 0
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 !
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 !
•
•
Join Date: Feb 2003
Posts: 129
Reputation:
Solved Threads: 1
Your C++ compiler comes with a random number generator, rand(). There's a tutorial on the site here:
http://www.daniweb.com/forums/thread1769.html
If you're producing a serious application you'll want something a little more sophisticated but for many purposes it's adequate.
http://www.daniweb.com/forums/thread1769.html
If you're producing a serious application you'll want something a little more sophisticated but for many purposes it's adequate.
Last edited by cscgal; Dec 8th, 2003 at 4:50 pm.
•
•
Join Date: May 2008
Posts: 9
Reputation:
Solved Threads: 0
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.
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.
Last edited by Bitto11; Jun 3rd, 2008 at 12:01 pm.
•
•
Join Date: Apr 2008
Posts: 296
Reputation:
Solved Threads: 42
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.
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 ...
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.
c++ Syntax (Toggle Plain Text)
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; }
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 ... Last edited by tesuji; Jun 3rd, 2008 at 7:21 pm. Reason: Is he still alive???
![]() |
Similar Threads
- using random number generator "globally" in a c++ class (C++)
- Gaussian Random number generator (C)
- Realy weird random number generator bug (C++)
- Random number generator (Java)
- Random number generator (Java)
- random number generator (C++)
Other Threads in the C++ Forum
- Previous Thread: access class members through pthread
- Next Thread: Delete Operation: Debug Assertion Failed (HEAP_CORRUPTION_DETECTED)
| Thread Tools | Search this Thread |
api array based binary c++ c/c++ calculator char char* class classes code coding compile console conversion count database delete deploy desktop developer directshow dll download dynamic dynamiccharacterarray email encryption error file forms fstream function functions game givemetehcodez google graph gui homeworkhelp iamthwee ifstream input int integer java lib linkedlist linker linux list loop looping loops map math matrix memory multiple news number numbertoword output parameter pointer problem program programming project python random read recursion recursive reference return rpg sorting string strings struct temperature template templates test text text-file tree unix url variable vector video visualstudio win32 windows winsock wordfrequency wxwidgets





