Random number generator's

Please support our C++ advertiser: Intel Parallel Studio Home
Reply

Join Date: Jul 2003
Posts: 3
Reputation: Matthew is an unknown quantity at this point 
Solved Threads: 0
Matthew Matthew is offline Offline
Newbie Poster

Random number generator's

 
0
  #1
Jul 17th, 2003
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
Reply With Quote Quick reply to this message  
Join Date: Jul 2003
Posts: 17
Reputation: odious face is an unknown quantity at this point 
Solved Threads: 0
odious face odious face is offline Offline
Newbie Poster

Re: Random number generator's

 
0
  #2
Jul 17th, 2003
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 !
Reply With Quote Quick reply to this message  
Join Date: Feb 2003
Posts: 129
Reputation: Bob is an unknown quantity at this point 
Solved Threads: 1
Team Colleague
Bob Bob is offline Offline
Team Member

Re: Random number generator's

 
0
  #3
Jul 18th, 2003
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.
Last edited by cscgal; Dec 8th, 2003 at 4:50 pm.
Reply With Quote Quick reply to this message  
Join Date: May 2008
Posts: 9
Reputation: Bitto11 is an unknown quantity at this point 
Solved Threads: 0
Bitto11 Bitto11 is offline Offline
Newbie Poster

Re: Random number generator's

 
0
  #4
Jun 3rd, 2008
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.
Reply With Quote Quick reply to this message  
Join Date: Nov 2007
Posts: 978
Reputation: mitrmkar is just really nice mitrmkar is just really nice mitrmkar is just really nice mitrmkar is just really nice mitrmkar is just really nice 
Solved Threads: 208
mitrmkar mitrmkar is offline Offline
Posting Shark

Re: Random number generator's

 
0
  #5
Jun 3rd, 2008
OP posted Jul 18th, 2003 ...
Reply With Quote Quick reply to this message  
Join Date: Apr 2008
Posts: 296
Reputation: tesuji is on a distinguished road 
Solved Threads: 42
tesuji tesuji is offline Offline
Posting Whiz in Training

Re: Random number generator's

 
0
  #6
Jun 3rd, 2008
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.
  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???
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC