943,908 Members | Top Members by Rank

Ad:
  • C++ Discussion Thread
  • Unsolved
  • Views: 620
  • C++ RSS
Mar 27th, 2009
0

Random Numbers

Expand 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.
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(). ?


C++ Syntax (Toggle Plain Text)
  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.
Similar Threads
Reputation Points: 10
Solved Threads: 1
Posting Whiz in Training
Lukezzz is offline Offline
268 posts
since Mar 2008
Mar 27th, 2009
0

Re: Random Numbers

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.
Reputation Points: 1234
Solved Threads: 347
Postaholic
ArkM is offline Offline
2,001 posts
since Jul 2008
Mar 27th, 2009
0

Re: Random Numbers

Click to Expand / Collapse  Quote originally posted by Lukezzz ...
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.
Reputation Points: 33
Solved Threads: 9
Junior Poster
DavidB is offline Offline
188 posts
since Jul 2006

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: Is this a proper use for extern?
Next Thread in C++ Forum Timeline: Problem with class/namespace? I'm going mad...





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


Follow us on Twitter


© 2011 DaniWeb® LLC