 |
C++Talk.NET C++ language newsgroups
|
| View previous topic :: View next topic |
| Author |
Message |
Guest
|
Posted: Wed Feb 01, 2006 9:00 pm Post subject: random numbers |
|
|
This is something i have done before and i know its pretty simple,
however i cant remember how it works exactly, and i need it i kinda
hurry, so if someone would be so nice to drop a random number generator
(actually i just need binary 1 or 0) i would be forever gratefull  |
|
| Back to top |
|
 |
Victor Bazarov Guest
|
Posted: Wed Feb 01, 2006 9:00 pm Post subject: Re: random numbers |
|
|
felixnielsen (AT) hotmail (DOT) com wrote:
| Quote: | This is something i have done before and i know its pretty simple,
however i cant remember how it works exactly, and i need it i kinda
hurry, so if someone would be so nice to drop a random number generator
(actually i just need binary 1 or 0) i would be forever gratefull ;-)
|
You probably can get away with calling 'rand' and comparing it by
RAND_MAX/2:
int randombit() {
return rand() > (RAND_MAX / 2);
}
V
--
Please remove capital As from my address when replying by mail |
|
| Back to top |
|
 |
Guest
|
Posted: Wed Feb 01, 2006 9:00 pm Post subject: Re: random numbers |
|
|
well, i guess thats good enough but i was thinking something like the
srand(time(0))...something more... |
|
| Back to top |
|
 |
TB Guest
|
Posted: Wed Feb 01, 2006 9:00 pm Post subject: Re: random numbers |
|
|
felixnielsen (AT) hotmail (DOT) com sade:
| Quote: | This is something i have done before and i know its pretty simple,
however i cant remember how it works exactly, and i need it i kinda
hurry, so if someone would be so nice to drop a random number generator
(actually i just need binary 1 or 0) i would be forever gratefull ;-)
|
#include <cstdlib>
int randombinary() {
return std::rand()%2;
}
int main() {
std::randomize();
return randombinary();
}
--
TB @ SWEDEN |
|
| Back to top |
|
 |
Guest
|
Posted: Wed Feb 01, 2006 9:00 pm Post subject: Re: random numbers |
|
|
well, it seems to work.
its probably a pseudo rand thingy, however as long as the sequence
length % 8 != 0 im satisfied.
Thanks for the help |
|
| Back to top |
|
 |
Victor Bazarov Guest
|
Posted: Wed Feb 01, 2006 11:00 pm Post subject: Re: random numbers |
|
|
TB wrote:
| Quote: | felixnielsen (AT) hotmail (DOT) com sade:
This is something i have done before and i know its pretty simple,
however i cant remember how it works exactly, and i need it i kinda
hurry, so if someone would be so nice to drop a random number generator
(actually i just need binary 1 or 0) i would be forever gratefull ;-)
#include <cstdlib
int randombinary() {
return std::rand()%2;
|
This is a BAD IDEA(tm). Read the C FAQ to learn why.
| Quote: | }
int main() {
std::randomize();
return randombinary();
}
|
V
--
Please remove capital As from my address when replying by mail |
|
| Back to top |
|
 |
Kai-Uwe Bux Guest
|
Posted: Wed Feb 01, 2006 11:01 pm Post subject: Re: random numbers |
|
|
Victor Bazarov wrote:
| Quote: | TB wrote:
felixnielsen (AT) hotmail (DOT) com sade:
This is something i have done before and i know its pretty simple,
however i cant remember how it works exactly, and i need it i kinda
hurry, so if someone would be so nice to drop a random number generator
(actually i just need binary 1 or 0) i would be forever gratefull ;-)
#include <cstdlib
int randombinary() {
return std::rand()%2;
This is a BAD IDEA(tm). Read the C FAQ to learn why.
|
Actually, the BAD IDEA is to use std::rand() at all: essentially you end up
using a pseudo random number generator of unknown quality -- and even if
the documentation tells you everything you want to known about std::rand()
on your platform, these guarantees do not extend to the next platform you
might want to run your code on: neither the C++ standard nor the C standard
make any useful warranties on std::rand(). As far as I can tell,
int rand ( void ) { return 0; }
is a compliant implementation.
As you pointed out, for some implementations, the low order bits might be
weak; but there is no assurance whatsoever that the high order bits on
another implementation are better. The warning about "%2" is just based on
the historical dominance of linear congruence RNGs, which may actually no
longer obtain.
To the OP: If you want portable code using a random number generator of
known quality, you should not use std::rand() but, for instance, the Boost
random library. It contains implementations of some state of the art random
number generators.
Best
Kai-Uwe Bux |
|
| Back to top |
|
 |
Default User Guest
|
Posted: Wed Feb 01, 2006 11:01 pm Post subject: Re: random numbers |
|
|
Victor Bazarov wrote:
| Quote: | TB wrote:
felixnielsen (AT) hotmail (DOT) com sade:
This is something i have done before and i know its pretty simple,
however i cant remember how it works exactly, and i need it i
kinda hurry, so if someone would be so nice to drop a random
number generator (actually i just need binary 1 or 0) i would be
forever gratefull ;-)
#include <cstdlib
int randombinary() {
return std::rand()%2;
This is a BAD IDEA(tm). Read the C FAQ to learn why.
|
Yes, very bad.
http://c-faq.com/lib/notveryrand.html
http://c-faq.com/lib/randrange.html
Brian |
|
| Back to top |
|
 |
Pete Becker Guest
|
Posted: Thu Feb 02, 2006 12:04 pm Post subject: Re: random numbers |
|
|
Kai-Uwe Bux wrote:
| Quote: |
Actually, the BAD IDEA is to use std::rand() at all: essentially you end up
using a pseudo random number generator of unknown quality -- and even if
the documentation tells you everything you want to known about std::rand()
on your platform, these guarantees do not extend to the next platform you
might want to run your code on: neither the C++ standard nor the C standard
make any useful warranties on std::rand().
|
So my quick-and-dirty dice-throwing program that gives perfectly
adequate results is really doing something awful behind the scenes?
| Quote: | As far as I can tell,
int rand ( void ) { return 0; }
is a compliant implementation.
|
Maybe, but it's a very poor quality implementation. Are you really
worried that some library somewhere does that?
| Quote: |
As you pointed out, for some implementations, the low order bits might be
weak; but there is no assurance whatsoever that the high order bits on
another implementation are better. The warning about "%2" is just based on
the historical dominance of linear congruence RNGs, which may actually no
longer obtain.
To the OP: If you want portable code using a random number generator of
known quality,
|
Now you're adding previously unstated requirements.
you should not use std::rand() but, for instance, the Boost
| Quote: | random library. It contains implementations of some state of the art random
number generators.
|
I have no qualms about using rand for most things. They dont't require
the overhead (both in code and in mental effort) of installing,
understanding, and using such a complex library.
--
Pete Becker
Dinkumware, Ltd. (http://www.dinkumware.com) |
|
| Back to top |
|
 |
Kai-Uwe Bux Guest
|
Posted: Thu Feb 02, 2006 1:01 pm Post subject: Re: random numbers |
|
|
Pete Becker wrote:
| Quote: | Kai-Uwe Bux wrote:
Actually, the BAD IDEA is to use std::rand() at all: essentially you end
up using a pseudo random number generator of unknown quality -- and even
if the documentation tells you everything you want to known about
std::rand() on your platform, these guarantees do not extend to the next
platform you might want to run your code on: neither the C++ standard nor
the C standard make any useful warranties on std::rand().
So my quick-and-dirty dice-throwing program that gives perfectly
adequate results is really doing something awful behind the scenes?
|
(a) Did you run checks to see whether the results are adequate? Or is that
just a hunch? Programs using RNGs are hard to judge because their behavior
looks, well, kind of random.
(b) Probably std::rand() does something awful: Any one who considers
arithmetical methods of producing random digits is, of course, in a state
of sin. (John von Neumann, quoted after Knuth: TAOCP Vol II)
(c) More important, though, std::rand() does something unknown. This is
sometimes acceptable, sometimes it is not.
| Quote: | As far as I can tell,
int rand ( void ) { return 0; }
is a compliant implementation.
Maybe, but it's a very poor quality implementation. Are you really
worried that some library somewhere does that?
|
No, I don't. But can you guarantee that the library is *significantly*
better. Sometimes people post code on this this list that they found used
in programs from their workplace to generate random numbers. More often
than not, I was flabbergasted at the poor quality of RNG code in use.
Clearly, I would hope that stuff like that does not make it into any C++ or
C library. However, can we be sure about this? Generally, I think, that
RNGs in libraries have improved quite a bit. In fact, I think that the
warning about low order bits may very well be no longer waranted. However,
if that warning is not outdated, one still needs to worry a lot about the
quality of std::rand(). I read that std::rand() on my Linux box with g++
does not suffer from low order bit weakness. Does anyone know about Windows
and compilers there?
| Quote: | As you pointed out, for some implementations, the low order bits might be
weak; but there is no assurance whatsoever that the high order bits on
another implementation are better. The warning about "%2" is just based
on the historical dominance of linear congruence RNGs, which may actually
no longer obtain.
To the OP: If you want portable code using a random number generator of
known quality,
Now you're adding previously unstated requirements.
|
No, I don't: did you miss the "If"? I even capitalized it:-)
Also, the OP did not state the specs, so we cannot really do better than
give conditional advice.
| Quote: | you should not use std::rand() but, for instance, the Boost
random library. It contains implementations of some state of the art
random number generators.
I have no qualms about using rand for most things. They dont't require
the overhead (both in code and in mental effort) of installing,
understanding, and using such a complex library.
|
Same here, but that is because I checked what rand() does on my machine:-)
Best
Kai-Uwe Bux |
|
| Back to top |
|
 |
Pete Becker Guest
|
Posted: Thu Feb 02, 2006 1:01 pm Post subject: Re: random numbers |
|
|
Kai-Uwe Bux wrote:
| Quote: | Pete Becker wrote:
Kai-Uwe Bux wrote:
Actually, the BAD IDEA is to use std::rand() at all: essentially you end
up using a pseudo random number generator of unknown quality -- and even
if the documentation tells you everything you want to known about
std::rand() on your platform, these guarantees do not extend to the next
platform you might want to run your code on: neither the C++ standard nor
the C standard make any useful warranties on std::rand().
So my quick-and-dirty dice-throwing program that gives perfectly
adequate results is really doing something awful behind the scenes?
(a) Did you run checks to see whether the results are adequate?
|
Of course not. If I had, it wouldn't be quick-and-dirty.
Or is that
| Quote: | just a hunch? Programs using RNGs are hard to judge because their behavior
looks, well, kind of random.
|
Yup. And often that's all that's needed.
| Quote: |
(b) Probably std::rand() does something awful: Any one who considers
arithmetical methods of producing random digits is, of course, in a state
of sin. (John von Neumann, quoted after Knuth: TAOCP Vol II)
|
But now you've proved too much, because that rules out almost all of the
random number generators in the TR1 library. There is a generator that
uses a hardware source if one is available, but when one isn't it falls
back on (gasp) arithmetical methods of producing random digits.
| Quote: |
(c) More important, though, std::rand() does something unknown. This is
sometimes acceptable, sometimes it is not.
|
Yup. Which is what I said in the first place.
| Quote: |
As far as I can tell,
int rand ( void ) { return 0; }
is a compliant implementation.
Maybe, but it's a very poor quality implementation. Are you really
worried that some library somewhere does that?
No, I don't. But can you guarantee that the library is *significantly*
better.
|
Have you seen any that aren't? Remember, we're talking here about
beginners getting something up and running. Adding complexity for the
sake of hypothetical issues just doesn't wash.
Sometimes people post code on this this list that they found used
| Quote: | in programs from their workplace to generate random numbers. More often
than not, I was flabbergasted at the poor quality of RNG code in use.
Clearly, I would hope that stuff like that does not make it into any C++ or
C library. However, can we be sure about this? Generally, I think, that
RNGs in libraries have improved quite a bit. In fact, I think that the
warning about low order bits may very well be no longer waranted. However,
if that warning is not outdated, one still needs to worry a lot about the
quality of std::rand(). I read that std::rand() on my Linux box with g++
does not suffer from low order bit weakness. Does anyone know about Windows
and compilers there?
|
Check and see. In the meantime, if you don't know, don't make sweeping
claims.
| Quote: |
As you pointed out, for some implementations, the low order bits might be
weak; but there is no assurance whatsoever that the high order bits on
another implementation are better. The warning about "%2" is just based
on the historical dominance of linear congruence RNGs, which may actually
no longer obtain.
To the OP: If you want portable code using a random number generator of
known quality,
Now you're adding previously unstated requirements.
No, I don't: did you miss the "If"? I even capitalized it:-)
|
No, I didn't miss it. I emphasized it: you're talking about a different
problem.
| Quote: | Also, the OP did not state the specs, so we cannot really do better than
give conditional advice.
you should not use std::rand() but, for instance, the Boost
random library. It contains implementations of some state of the art
random number generators.
I have no qualms about using rand for most things. They dont't require
the overhead (both in code and in mental effort) of installing,
understanding, and using such a complex library.
Same here, but that is because I checked what rand() does on my machine:-)
|
I haven't checked, and I won't until it matters.
--
Pete Becker
Dinkumware, Ltd. (http://www.dinkumware.com) |
|
| Back to top |
|
 |
Kai-Uwe Bux Guest
|
Posted: Thu Feb 02, 2006 2:00 pm Post subject: Re: random numbers |
|
|
Pete Becker wrote:
| Quote: | Kai-Uwe Bux wrote:
Pete Becker wrote:
Kai-Uwe Bux wrote:
[snip]
As far as I can tell,
int rand ( void ) { return 0; }
is a compliant implementation.
Maybe, but it's a very poor quality implementation. Are you really
worried that some library somewhere does that?
No, I don't. But can you guarantee that the library is *significantly*
better.
Have you seen any that aren't?
|
I also have never actually seen a standard library(!) that was suffering
from low order bit weakness. I only heard rumors about them.
| Quote: | Remember, we're talking here about
beginners getting something up and running. Adding complexity for the
sake of hypothetical issues just doesn't wash.
|
Actually, we are not. We are talking about the problem of realinzing a fair
coin in C++. Where did you get the idea about beginners being the only
people supposed to benefit from reading posts in this thread?
| Quote: | Sometimes people post code on this this list that they found used
in programs from their workplace to generate random numbers. More often
than not, I was flabbergasted at the poor quality of RNG code in use.
Clearly, I would hope that stuff like that does not make it into any C++
or C library. However, can we be sure about this? Generally, I think,
that RNGs in libraries have improved quite a bit. In fact, I think that
the warning about low order bits may very well be no longer waranted.
However, if that warning is not outdated, one still needs to worry a lot
about the quality of std::rand(). I read that std::rand() on my Linux box
with g++ does not suffer from low order bit weakness. Does anyone know
about Windows and compilers there?
Check and see.
|
Thanks for your helpful advice: I would love to, but I am not running
Windows on any of my machines. I was just honestly asking a question.
| Quote: | In the meantime, if you don't know, don't make sweeping claims.
|
Well, what about the claim about the low order bits in the C-FAQ then which
I have issues with in the first place? Most of what I am saying is that
that particular warning might need a reasessment (although I lack the
resources to carry that out). And the second part is, if you want to be
safe, use some RNG that you know. You are just saying: Do not desire to be
safe.
| Quote: | To the OP: If you want portable code using a random number generator of
known quality,
Now you're adding previously unstated requirements.
No, I don't: did you miss the "If"? I even capitalized it:-)
No, I didn't miss it. I emphasized it: you're talking about a different
problem.
|
As I already said, the OP did not give any specs (other than that he is in a
hurry). You simply don't know whether this is a different problem or not.
Best
Kai-Uwe Bux |
|
| Back to top |
|
 |
Pete Becker Guest
|
Posted: Thu Feb 02, 2006 4:00 pm Post subject: Re: random numbers |
|
|
Kai-Uwe Bux wrote:
| Quote: |
Remember, we're talking here about
beginners getting something up and running. Adding complexity for the
sake of hypothetical issues just doesn't wash.
Actually, we are not. We are talking about the problem of realinzing a fair
coin in C++. Where did you get the idea about beginners being the only
people supposed to benefit from reading posts in this thread?
|
The original question was obviously from a beginner.
| Quote: |
Well, what about the claim about the low order bits in the C-FAQ then which
I have issues with in the first place?
|
I didn't write it and I don't cite it.
| Quote: |
As I already said, the OP did not give any specs (other than that he is in a
hurry).
|
Sure he did: he needs a random number generator that gives 0s and 1s.
And in reply to the message from TB, which suggested rand()%2, he said
"I guess thats good enough" (except for the issue of seeding).
| Quote: | You simply don't know whether this is a different problem or not.
|
On the contrary: this adds a requirement that wasn't stated in the
original message nor in the followup. Before adding the complexity of a
large library like TR1 (or the Boost version of the random number
generators) it's essential to know what problem is being solved. It's
certainly fashionable in newsgroups (sweeping statement, not intended to
apply only to this thread) to give detailed advice about every possible
issue that might arise, but that's usually counterproductive. Do you
think the original poster is still reading this discussion?
--
Pete Becker
Dinkumware, Ltd. (http://www.dinkumware.com) |
|
| Back to top |
|
 |
Tom Guest
|
Posted: Thu Feb 02, 2006 4:00 pm Post subject: Re: random numbers |
|
|
On 1 Feb 2006 12:02:21 -0800, felixnielsen (AT) hotmail (DOT) com wrote:
| Quote: | This is something i have done before and i know its pretty simple,
however i cant remember how it works exactly, and i need it i kinda
hurry, so if someone would be so nice to drop a random number generator
(actually i just need binary 1 or 0) i would be forever gratefull
|
Testing of random number generators often shows how poorly the many
varieties actually perform. You want a flat distribution profile
across the entire region. I typically use 100,000 as a minimum sample
size and use at least 20 bins to collect the sums during the test.
One of the best and free random generators I've used is the Mersenne
Twister algorithm. There are a few bugs in that program though and you
should thoroughly test how you implement it until you get stable
results. For example, you should fully prime the Seed array and not
use the buggy auto priming routines provided in the program. That is
"if" you want repeatable results from a particular starting value.
In the past I have found betting profiles that beat the game of craps
on a simulated 30,000 rolls of the dice while using one of the poor
performing and readily available generators. By obtaining a more
robust generator and increasing the sample size to 100,000
descisions(vs rolls) ... the "winning" patterns all disappeared. Thus
the "reality" of the game became apparent.
Moral of the craps story: Don't believe the results unless you
thoroughly test the random number generator and use a sufficient
sample size. If what you are doing is not "critically important" ...
then a simplified random number generator is sufficient. For example,
the starting value of a video game is not critical because you take
over control asap.
Randomness is fascinating. Creating a high quality generator is a very
tough challenge.
Happy simulating.
-- Tom |
|
| Back to top |
|
 |
Kai-Uwe Bux Guest
|
Posted: Thu Feb 02, 2006 11:00 pm Post subject: Re: random numbers |
|
|
Pete Becker wrote:
| Quote: | Kai-Uwe Bux wrote:
Remember, we're talking here about
beginners getting something up and running. Adding complexity for the
sake of hypothetical issues just doesn't wash.
Actually, we are not. We are talking about the problem of realinzing a
fair coin in C++. Where did you get the idea about beginners being the
only people supposed to benefit from reading posts in this thread?
The original question was obviously from a beginner.
|
So what? He is clearly not the only one reading this thread. Threads develop
a life of their own; and now that most of these discussions are googlabel,
the OP cannot be regarded the target audience of any post deeply nested in
a subthread. My first post in this subthread was explicitly only in part
directed to the OP.
| Quote: | As I already said, the OP did not give any specs (other than that he is
in a hurry).
Sure he did: he needs a random number generator that gives 0s and 1s.
And in reply to the message from TB, which suggested rand()%2, he said
"I guess thats good enough" (except for the issue of seeding).
|
If he is a beginner, as you think, his "guess" might be wrong and other
posts may help him to make a guess that is at least better informed.
| Quote: | You simply don't know whether this is a different problem or not.
On the contrary: this adds a requirement that wasn't stated in the
original message nor in the followup. Before adding the complexity of a
large library like TR1 (or the Boost version of the random number
generators) it's essential to know what problem is being solved. It's
certainly fashionable in newsgroups (sweeping statement, not intended to
apply only to this thread) to give detailed advice about every possible
issue that might arise, but that's usually counterproductive.
|
I could not disagree more. This is probably the heart of the matter. You and
I simply have a different understanding of what purpose news group
discussions serve. I am willing to agree to disagree on this matter.
Best
Kai-Uwe Bux |
|
| Back to top |
|
 |
|
|
You cannot post new topics in this forum You cannot reply to topics in this forum You cannot edit your posts in this forum You cannot delete your posts in this forum You cannot vote in polls in this forum
|
|