Random value

May 25, 2012 at 11:31am
Hi

I want to generate random values, so i used rand() from <stdlib.h>.
The problem that i got every time the same value.
i don't knwo what is the problem.
So are there any method to reintile the buffer userd to generate random value so i will got different values in every execution or another solution ?

Thank you in advance.
May 25, 2012 at 11:45am
rand does not actually give you random numbers. It gives you numbers from a set sequence. If you use the same sequence every time, you will get the same numbers.

You change the sequence using the srand function. At the start of your code, use srand once to set the sequence.
May 25, 2012 at 11:50am
If you're using a more recent compiler (like the one in Visual Studio 2010, or GNU G++ 4.6+) you might want to look into C++11's new random library:

http://en.cppreference.com/w/cpp/numeric/random
http://en.cppreference.com/w/cpp/numeric/random/uniform_int_distribution
May 25, 2012 at 2:01pm
The C++ random library is pretty old now: it first appeared as boost.random in 2000, got accepted into C++TR1 in 2005 and into the standard in 2011.

MS supports it since Visual Studio 2008 and GNU g++ since version 4.2.

Although the generators it provides will produce the same numbers from the same seeds anyway.
Topic archived. No new replies allowed.