Random Numbers

W

W Marsh

Dirtydog said:
Can you please type out a full line of code
Please make the effort to learn the language well enough to call a
simple function when you have been given its name. Anybody would think
that Google cost $10 per search by the way you people go on. Are you
really so mentally dull that you can't show any initiative?
 
R

Robert J. Hansen

In c++ what is the code to make teh program randomly select a number?

Effectively, there isn't. rand() has a lot of very bad, bad
properties. It's generally useless for cryptography, for Monte Carlo
simulations, for randomized algorithms, for... etcetera.

If you know that you only need very low-quality random numbers, use
rand(). If you need anything better quality, use your operating
system's built-in facilities (usually CryptGenRandom or /dev/urandom).
 
G

Greg

Victor said:
Usually we employ the help of 'rand' function from the C library.

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.

Greg
 
P

Pete Becker

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.

If it's available and it doesn't return pseudo-random numbers. It's
allowed to, so check the documentation.
 
P

Pete Becker

Robert said:
Effectively, there isn't. rand() has a lot of very bad, bad
properties.

Where in the standard is this requirement? <g>

Many years ago, there were many bad implementations of rand. There still
may be.
 
R

Robbie Hatley

In c++ what is the code to make the program randomly select a number?

Here's some functions I wrote a while back to get decent pseudo-random
integers and doubles:

First, to seed the random number generator by time (which makes the
pseudo-random numbers less repeatable, and hence less "pseudo"), run
THIS function *ONCE ONLY* at the beginning of your main():

#include <cmath>
inline void Randomize(void)
{
srand(time(0));
}

Now a function to get a pseudo-random double within a given range:

// 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.
For example, to get equal probabilities for the interval [5,9], you
need to feed min=5.001, max=9.999 to RandNum. Thus the range of
doubles corresponding to 9 is about 1. (If you had fed the more
intuitive value of 9.000 to RandNum, then 9's probability would
be 0.000%, not 20.000% as it should be.)

2. Roundoff errors. When casting back and forth between double and int,
it's easy to accidentally end up with a number that's 1 less or more
than it should be. Hence I add a saftey margin of 0.001.

--
Cheers,
Robbie Hatley
East Tustin, CA, USA
lone wolf intj at pac bell dot net
(put "[usenet]" in subject to bypass spam filter)
home dot pac bell dot net slant earnur slant
 
P

Pete Becker

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)

change that last bit to (static_cast<double>(RAND_MAX) + 1). RAND_MAX is
the maximum value that rand() will return. As written, RandNum returns
max only when rand() returns RAND_MAX. Other values come up more often.
);
}

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.

Only if the floating-point values that you're working with are skewed.
With the change I suggested above, removing the adjustments here will
produce values in the half-open interval [min,max] that are as well
distributed as the values returned by rand().
 
F

Frederick Gotham

Greg posted:
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.


While we're being pedantic -- there's no such thing as a random number.
 
F

Frederick Gotham

Robbie Hatley posted:
inline double RandNum(double min, double max)
{
return min + (max - min) * (
static_cast<double>(rand())
/
static_cast<double>(RAND_MAX)
);
}


The twin casts are slight overkill (in my opinion):

inline
double RandNum(double const min,double const max)
{
return (max-min) * rand() / (RAND_MAX+1) + min;
}

Or, if you would prefer the division to take place before the multiplication:

return (max-min) * (rand() / (double)(RAND_MAX+1)) + min;
 
J

Jerry Coffin

[ ... ]
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.
 
G

Greg Comeau

[ ... ]
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.

IOWs, random is random :)
 
F

Frederick Gotham

Kai-Uwe Bux posted:
Sure there is, for instance 2. <g>


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. ;)
 
K

Kai-Uwe Bux

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. ;)

If you want to choose a random order of a deck of cards, then you could
shuffle the deck and put down all cards in order, or you could shuffle,
remove a card and put it down, shuffle again, put down a card, shuffle,
put, ... until all cards are put down. Both procedures are strictly
equivalent. In other words: there is no difference between a sequence of
random events and a determined destiny chosen randomly in the beginning. In
a deterministic universe, the big bang just chose a destiny for the world,
but it may have done so randomly.


Best

Kai-Uwe Bux
 
M

Markus Schoder

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.

Yes, but only in _this_ universe ;)
 

Ask a Question

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.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,534
Members
45,008
Latest member
Rahul737

Latest Threads

Top