| | |
Random Numbers
Please support our C++ advertiser: Intel Parallel Studio Home
![]() |
•
•
Join Date: Mar 2008
Posts: 173
Reputation:
Solved Threads: 1
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(). ?
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)
int Number5 = 0; System::String^ text; for( int i = 0; i < 20; i++ ) { Number5 = rand() % (51) + 0; stringstream v1; std::string v2; v1 << Number5; v2 = v1.str(); String^ Number = gcnew String(v2.c_str()); this->textBox1->Text = text; this->textBox1->Multiline = true; this->textBox1->WordWrap = false; text += Number + System::Environment::NewLine; }
Last edited by Lukezzz; Mar 27th, 2009 at 6:42 pm.
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
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/
). 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.
•
•
Join Date: Jul 2006
Posts: 88
Reputation:
Solved Threads: 2
•
•
•
•
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.
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.
![]() |
Similar Threads
- Compile time errors in C++ while generating random numbers (C++)
- C++ Random Numbers (C++)
- C++ Reorder random numbers (C++)
- random numbers all different??? (C++)
Other Threads in the C++ Forum
- Previous Thread: Is this a proper use for extern?
- Next Thread: Problem with class/namespace? I'm going mad...
| Thread Tools | Search this Thread |
api array arrays based beginner binary c++ c/c++ calculator char class classes code compile compiler console conversion count delete deploy desktop directshow dll download dynamic dynamiccharacterarray encryption error file forms fstream function functions game getline givemetehcodez google graph gui homeworkhelp homeworkhelper iamthwee ifstream input int integer java lib linkedlist linker linux list loop looping loops map math matrix memory news number output parameter pointer problem program programming project proxy python random read recursion recursive reference return rpg string strings struct temperature template templates test text text-file tree unix url variable vector video visual visualstudio win32 windows winsock word wordfrequency wxwidgets






