-
November 26th, 2014, 03:27 PM
#1
Why are my random values not very random?
Hey all,
I've been working on a little experiment, here is the source:
https://gist.github.com/anonymous/82df1005a20f54520330
https://gist.github.com/anonymous/3d3f5b7599b425bbc82f
https://gist.github.com/anonymous/4580ea675dcff1c4cf2f
https://gist.github.com/anonymous/fdcd5504d6936d8953c4
https://gist.github.com/anonymous/5ac50c714b908fa05418
https://gist.github.com/anonymous/4ae14e49aca692f390ea
https://gist.github.com/anonymous/5a3df0f6d2c5c376ae07
The problem that I keep running into is when I run the initPop and generate an individual object, the genome of the next individual is _exactly_ the same as the previous one... which confuses me... Shouldn't each individual be randomly different from the one that preceded it? What am I not right when it comes to generating random values?
-
November 26th, 2014, 03:40 PM
#2
Re: Why are my random values not very random?
Please post your code rather than providing links. Myself and other members don't open unknown links.
If the program is large, produce a small test program that demonstrates the problem and just post that.
All advice is offered in good faith only. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only - not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/ and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums - and not via private messages!
C++23 Compiler: Microsoft VS2022 (17.6.5)
-
November 26th, 2014, 03:54 PM
#3
Re: Why are my random values not very random?
I didn't read the code.
If you are getting the same result every *run*, then your forgot to srand.
If you are experiencing duplicate sequences in a single run, then you are probably multi-srand'ing, which will reset your rand (even if you use clock: computers are so fast entire programs can run without clock ever changing).
Long story short: if you are using rand, there needs to be 1 (and *only* 1) call to srand, in main, and nowhere else.
If you are using new-style C++11 random *objects*, then I suggest you use a global seed_seq to seed those individual objects. Using the clock more than once in a program for seeding *will* lead to pain.
Is your question related to IO?
Read this C++ FAQ article at parashift by Marshall Cline. In particular points 1-6.
It will explain how to correctly deal with IO, how to validate input, and why you shouldn't count on "while(!in.eof())". And it always makes for excellent reading.
-
November 26th, 2014, 04:04 PM
#4
Re: Why are my random values not very random?
Originally Posted by monarch_dodra
I didn't read the code.
If you are getting the same result every *run*, then your forgot to srand.
If you are experiencing duplicate sequences in a single run, then you are probably multi-srand'ing, which will reset your rand (even if you use clock: computers are so fast entire programs can run without clock ever changing).
Long story short: if you are using rand, there needs to be 1 (and *only* 1) call to srand, in main, and nowhere else.
If you are using new-style C++11 random *objects*, then I suggest you use a global seed_seq to seed those individual objects. Using the clock more than once in a program for seeding *will* lead to pain.
*blink*
You're right. I was using multiple srands. Why was this happening? It doesn't make any sense.
-
November 26th, 2014, 04:05 PM
#5
Re: Why are my random values not very random?
Originally Posted by 2kaud
Please post your code rather than providing links. Myself and other members don't open unknown links.
If the program is large, produce a small test program that demonstrates the problem and just post that.
I'll attach the code next time.
But the site in question is github, that's a pretty solid site.
-
November 26th, 2014, 04:47 PM
#6
Re: Why are my random values not very random?
Originally Posted by YourSurrogateGod
*blink*
You're right. I was using multiple srands. Why was this happening? It doesn't make any sense.
srand "(re)initialises" your global rand sequence using the seed you give it. If you are using clock, then chances are the clock did not actually change, so you are effectively "resetting" your rand sequence.
Try it:
Code:
int main()
{
for (int i = 0; i < 10; ++i)
{
srand(time(NULL));
for (int i = 0; i < 10; ++i)
{
std::cout << rand() << " ";
}
std::cout << std::endl;
}
}
This simplified code is vulnerable, and has very high chances of producing the same sequence more than once. If instead of generating numbers, you are generating a sequence, then the same thing could happen.
Is your question related to IO?
Read this C++ FAQ article at parashift by Marshall Cline. In particular points 1-6.
It will explain how to correctly deal with IO, how to validate input, and why you shouldn't count on "while(!in.eof())". And it always makes for excellent reading.
-
November 28th, 2014, 08:47 AM
#7
Re: Why are my random values not very random?
also:
the normal C-library rand() is TERRIBLE for just about every usage case.
it has a 32K period, only 15 bits of 'randomness', and it has a very high predictability and artefact generation.
-
November 28th, 2014, 03:59 PM
#8
Re: Why are my random values not very random?
Originally Posted by OReubens
also:
the normal C-library rand() is TERRIBLE for just about every usage case.
The rand() is not that bad and good enougth, even preferred, for most non-scientific applications.
The OPs problem isn't the quality of the statistical properties of the random number generator but the question of how to create a true chance event on a computer. The usual trick is to use the built-in timer and that won't change whatever random number generator one is using, be it rand() or something more fancy.
In fact if you want to simulate a physical dice then rand() is a good choise. It's kind of the dice standard for computers. As you know a physical dice is imperfect and the more perfect the random number generator the less it will resemble a physical dice. In fact using a too good random number generator to simulate a dice poses a problem, it behaves too "mathematically" and doesn't show the "skewedness" and "loadedness" of a real dice.
I've been using dice as an example but it extends to most non-scientific simulation of everyday events. Here rand() is fine and, as I said, something of a standard. It's only if you do hardcore scientific work you need the new <random> library of C++ 11. You don't need a Rolls Royce to transport dung.
http://www.eternallyconfuzzled.com/a..._art_rand.aspx
Last edited by razzle; November 29th, 2014 at 05:46 AM.
-
November 29th, 2014, 11:09 AM
#9
Re: Why are my random values not very random?
Originally Posted by razzle
In fact if you want to simulate a physical dice then rand() is a good choise. It's kind of the dice standard for computers. As you know a physical dice is imperfect and the more perfect the random number generator the less it will resemble a physical dice. In fact using a too good random number generator to simulate a dice poses a problem, it behaves too "mathematically" and doesn't show the "skewedness" and "loadedness" of a real dice.
I call BS on that statement. All random number generators generate randomness with varying "quality", but *none* will emulate something such as "skewedness" or "loadedness". They will all generate numbers uniformly on their valid range. rand has higher chances of showing predictable bit patterns, but that's the exact contrary of "too mathematical". TLDR: A PRNG's "low" quality does not emulate real-world imperfection. On the contrary, it injects artificial correlation.
The biggest issue I've encountered with rand implemented as an LCG is that it is VERY vulnerable to modulo mathematics. I don't remember the exact details, but I remember at least one dice program I wrote that threw N dices, that completely went to hell when asked to operate on 16 (or generally 2^N) dices.
Also, "rand" promotes using modulo to get a number between 0 and N. Because rand usually has a low RAND_MAX, doing something such as "rand() % N" can give you some seriously non-uniform distribution. And there is nothing built-in in C's stdlib to avoid it. C++11 not only gives you higher quality generators, but also distributions. You really want to use those.
I agree that on toy projects, you might get away with using "rand()", but even then, it's not guaranteed. But evoking "hardcore scientific" is a fallacy I believe. If you have access to something better, there really is not a lot to justify sticking with rand. Mersenne Twister is hardly a "Rolls Royce". I'd say it's more like an everyday car, and rand a donkey pulled leaky chariot.
Is your question related to IO?
Read this C++ FAQ article at parashift by Marshall Cline. In particular points 1-6.
It will explain how to correctly deal with IO, how to validate input, and why you shouldn't count on "while(!in.eof())". And it always makes for excellent reading.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|