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

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;
		}

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/

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.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.