Random Numbers

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

Join Date: Mar 2008
Posts: 173
Reputation: Lukezzz is an unknown quantity at this point 
Solved Threads: 1
Lukezzz Lukezzz is offline Offline
Junior Poster

Random Numbers

 
0
  #1
Mar 27th, 2009
I use rand() to generate random numbers between 0-50.
However when I use the code, as I have seen it, rand() doesn´t truly generate random numbers.
It seems that everytime I restart the application, it generates the same random numbers everytime.
It seems that there is any mathematical sequence the function goes through and by that generate "random" numbers.

Is there any better method or function that is closer to generate random numbers than rand(). ?


  1. int Number5 = 0;
  2.  
  3. System::String^ text;
  4.  
  5. for( int i = 0; i < 20; i++ )
  6. {
  7. Number5 = rand() % (51) + 0;
  8.  
  9.  
  10. stringstream v1;
  11. std::string v2;
  12. v1 << Number5;
  13. v2 = v1.str();
  14. String^ Number = gcnew String(v2.c_str());
  15.  
  16. this->textBox1->Text = text;
  17. this->textBox1->Multiline = true;
  18. this->textBox1->WordWrap = false;
  19.  
  20.  
  21. text += Number + System::Environment::NewLine;
  22. }
Last edited by Lukezzz; Mar 27th, 2009 at 6:42 pm.
Reply With Quote Quick reply to this message  
Join Date: Jul 2008
Posts: 2,001
Reputation: ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of 
Solved Threads: 343
ArkM's Avatar
ArkM ArkM is offline Offline
Postaholic

Re: Random Numbers

 
0
  #2
Mar 27th, 2009
1. The rand function never generates "random" numbers. It generates pseudo random numbers (feel the difference ). There is a very interesting (and complex) mathematical theory on pseudo random sequences. See, for example:
http://en.wikipedia.org/wiki/Random_number_generator
Apropos: it's not so easy to generate "true" random numbers on such deterministic device as a digital computer ...
2. Use srand(time(0)) call to initiate different pseudorandom sequences.
3. Never use the rand library function in serious applications. Nobody knows why but all known programming language implementations have a very bad pseudorandom generators . There are some good pseudorandom generators. One of the best is so called Mersenne Twister. There are lots of its freeware C and C++ implementations. See, for example:
http://www.bedaux.net/mtrand/
Last edited by ArkM; Mar 27th, 2009 at 7:07 pm.
Reply With Quote Quick reply to this message  
Join Date: Jul 2006
Posts: 88
Reputation: DavidB is an unknown quantity at this point 
Solved Threads: 2
DavidB DavidB is offline Offline
Junior Poster in Training

Re: Random Numbers

 
0
  #3
Mar 27th, 2009
Originally Posted by Lukezzz View Post
I use rand() to generate random numbers between 0-50.
However when I use the code, as I have seen it, rand() doesn´t truly generate random numbers.
It seems that everytime I restart the application, it generates the same random numbers everytime.
True. That is so results can be repeated. For example, if you are using random numbers to fill a matrix for some program you are debugging, you would like the data to be the same each time you re-run the program.

To get more random numbers, you could always use a random number generator that accepts a seed value, and then feed the routine a different seed value each time you re-run it. Or you could use one that creates its own initial value based on ambient temperature, your typing speed, the computer clock, etc.
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