E
Elfour
In c++ what is the code to make teh program randomly select a number?
In c++ what is the code to make teh program randomly select a number?
Victor said:Usually we employ the help of 'rand' function from the C library.
V
Please make the effort to learn the language well enough to call aDirtydog said:Can you please type out a full line of code
Can you please type out a full line of code
Victor said:Usually we employ the help of 'rand' function from the C library.
Greg said:A truly random number would have to obtained in a non-deterministic
way. In that case std::tr1::random_device (assuming it is available for
your compiler) would provide an actual random number.
Robert said:Effectively, there isn't. rand() has a lot of very bad, bad
properties.
In c++ what is the code to make the program randomly select a number?
Robbie said:// Get random double:
inline double RandNum(double min, double max)
{
return min + (max - min) * (
static_cast<double>(rand())
/
static_cast<double>(RAND_MAX)
);
}
And finally, a function to get a pseudo-random integer within a given range:
// Get random int:
inline int RandInt(int min, int max)
{
return static_cast<int>(RandNum(min + 0.001, max + 0.999));
}
If you're wondering about the 0.001 and 0.999, they're necessary to prevent
two different kinds of error:
1. Fencepost errors. If you don't carfully skew the end points, the minimum
and maximum integers of your range will have dramatically different
probability of occurring than any of the other integers in the range.
But rand() returns a "pseudo-random" number. Each number returned
depends on the previous one. The entire sequence that rand() returns
only appears to be random. In reality the series is deterministic and
will start to repeat itself after rand() has been called enough times.
A truly random number would have to obtained in a non-deterministic
way. In that case std::tr1::random_device (assuming it is available for
your compiler) would provide an actual random number.
inline double RandNum(double min, double max)
{
return min + (max - min) * (
static_cast<double>(rand())
/
static_cast<double>(RAND_MAX)
);
}
Greg posted:
While we're being pedantic -- there's no such thing as a random number.
While we're being pedantic -- there's no such thing as a random number.
[ ... ]
While we're being pedantic -- there's no such thing as a random number.
This is mostly a question of how you define your terms. At least IMO,
however, as an unqualified statement, it's false. There are things such
as radioactive decay that, at least with our current knowledge of
physics, are completely unpredictable. When done properly, masurements
of such things can produce, for example, a series of bits that is
"random" for any practical definition of the term -- i.e. nobody seems
to have found a way to predict the next bit that will be produced with
any better than 50% accuracy -- i.e. random chance.
Beyond that, what you choose to say about things is mostly a question of
how you prefer to define your terms.
Frederick said:Greg posted:
While we're being pedantic -- there's no such thing as a random number.
Sure there is, for instance 2. <g>
Frederick said:Kai-Uwe Bux posted:
When the Big Bang occurred, molecules and atoms and so forth got flung out
in ever direction -- the resultant collisions and changes in trajectory
resulted in the formation of you as an organism, and resulted in your
choice of 2.
So you see, it wasn't random.
Frederick said:Kai-Uwe Bux posted:
When the Big Bang occurred, molecules and atoms and so forth got flung
out in ever direction -- the resultant collisions and changes in
trajectory resulted in the formation of you as an organism, and
resulted in your choice of 2.
Want to reply to this thread or ask your own question?
You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.