943,289 Members | Top Members by Rank

Ad:
  • C++ Discussion Thread
  • Unsolved
  • Views: 34342
  • C++ RSS
Jul 17th, 2003
0

Random number generator's

Expand Post »
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
Similar Threads
Reputation Points: 11
Solved Threads: 0
Newbie Poster
Matthew is offline Offline
3 posts
since Jul 2003
Jul 17th, 2003
0

Re: Random number generator's

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 !
Reputation Points: 10
Solved Threads: 1
Newbie Poster
odious face is offline Offline
17 posts
since Jul 2003
Jul 18th, 2003
0

Re: Random number generator's

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

http://www.daniweb.com/forums/showthread.php?t=1769

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.
Bob
Team Colleague
Reputation Points: 15
Solved Threads: 2
Junior Poster
Bob is offline Offline
129 posts
since Feb 2003
Jun 3rd, 2008
0

Re: Random number generator's

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.
Last edited by Bitto11; Jun 3rd, 2008 at 12:01 pm.
Reputation Points: 10
Solved Threads: 0
Newbie Poster
Bitto11 is offline Offline
9 posts
since May 2008
Jun 3rd, 2008
0

Re: Random number generator's

OP posted Jul 18th, 2003 ...
Reputation Points: 1105
Solved Threads: 389
Posting Virtuoso
mitrmkar is offline Offline
1,713 posts
since Nov 2007
Jun 3rd, 2008
0

Re: Random number generator's

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.
c++ Syntax (Toggle Plain Text)
  1. unsigned long int lehmer(long int s)
  2. // linear congruential pseudo random number generator based on D. Lehmer
  3. { static unsigned long long a = 2007, b = 4194301, c = 2147483647, z = b;
  4. if ( s < 0 ) {s = -s; a = s;}
  5. z = (a + b * z) % c;
  6. return z % s;
  7. }
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 ...
Last edited by tesuji; Jun 3rd, 2008 at 7:21 pm. Reason: Is he still alive???
Reputation Points: 158
Solved Threads: 98
Master Poster
tesuji is offline Offline
720 posts
since Apr 2008

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C++ Forum Timeline: access class members through pthread
Next Thread in C++ Forum Timeline: Delete Operation: Debug Assertion Failed (HEAP_CORRUPTION_DETECTED)





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC