# random isn't really random?

This is a discussion on random isn't really random? within the C++ Programming forums, part of the General Programming Boards category; hi, Just a quick question. I wrote a short code for generating 0, 1 randomly in order to simulate a ...

1. ## random isn't really random?

hi,
Just a quick question. I wrote a short code for generating 0, 1 randomly in order to simulate a coin-tossing game.
Code:
```//This program will simulate 100 times
//of coin-tossings. Then it will calculate
//the frequency how many times it's HEAD or TAIL
#include <iostream>
#include <iomanip>
#include <cstdlib>
using namespace std;
int flip(void); //return 0 for T(tail=0) and H(head=1)

int main()
{
int tailCount=0;
for (int i=1; i<=100; i++) //simulate flipping 100 times
{
switch(flip())
{
case (1):
cout << " " << "H";
break;
case (0):
cout << " " << "T";
tailCount++;
break;
}
if (i%10==0)
cout << endl;
}
cout << "Tail " << tailCount << endl;

return 0;
}
//Function that does fliping th coin
int flip(void)
{
int outcome;
outcome=rand()%2;
return outcome;
}```
I compiled and execute the program, it just gave out 1 result all the time!!!
I'm wondering probably the way I use rand() is not good enough?

thanks!

2. Do you mean that you get 100 of the same result, or that every time you run the program you get the same split of heads/tails?

The latter is expected -- the pseudo random number generator is designed to be "predictable" in this way (so that you can check that you don't break things as you're debugging). To get different numbers on different runs of the program, you need to use srand() to set a seed. (Usually you do something like srand(time(0)); this assumes that you #include time.h.)

3. I got the same result
H T T T T H H H H
T T T H H H H H H
......

and the same count for each H and T for every execution...
perhaps, I should use "srand"

4. Right, you're doing flips without seeding your random. so be sure to run seed(time(NULL)) and it will change output.

With that, dig into that faq.

5. Originally Posted by azsquall
I got the same result
H T T T T H H H H
T T T H H H H H H
......

and the same count for each H and T for every execution...
perhaps, I should use "srand"
Yes, if you want each time you run the code to produce different results, you should use some sort of random seed that varies from time to time.

All of the traditional random number generators that are used by computers generate a series of numbers based on a "seed". If you start with the same seed, you get the same sequence. If you start with very similar seeds, it's likely that you get a similar result - but by no means guaranteed.

So running your code twice in a row using time(0) as a seed may produce a very similar result, because the "time" may be close enough to produce the same result. If you wait for several seconds before running it again, then you would be more safe.

--
Mats

6. heh. welcome to the world of PRNGs.

QRBG is the way to go Quantum Random Bit Generator

kidding . srand(time(NULL)) would probably work ok for you, though I found that one needs to wait at least 1 second between each call, and even then the results were largely linear.

7. You should only call srand once in your program. Once the PRNG is seeded it will generate pseudo-random numbers properly. If you keep seeding it that will throw off its mechanism.

8. Originally Posted by Daved
You should only call srand once in your program. Once the PRNG is seeded it will generate pseudo-random numbers properly. If you keep seeding it that will throw off its mechanism.
like I mentioned in the QRBG post, I seeded srand() with the output of the Mersenne PRNG, and I did this for every iteration of the loop. that works very well.

9. Yeah, but it makes no sense (I admit I didn't read your entire post in that thread or I would have mentioned it there). The point of a seed is to seed it once, it will generate a proper random number after that. If you're seeding rand every time, then your random number is based only on the Mersenne PRNG, and there isn't any random-ness derived from rand(). You might as well just use the output of the Mersenne PRNG directly.

10. might wanto to read this

11. I changed your program with srand() but i get
"void value not ignored as it ought to be" error.

Code:
```int flip(void)
{
int outcome;
outcome = srand ( time(0) &#37; 2 );   //error
return outcome;
}```
I tried to google it without success.

12. Completely confusing error. Dev-C++ can be so unhelpful.
A better error is "cannot convert from void to int," or in other words: srand does not return anything so you can't assign its return to anything.
And you can drop the "void" in the function header. It's not required in C++.

13. Completely confusing error. Dev-C++ can be so unhelpful.
A better error is "cannot convert from void to int,"
I guess this depends on which compiler's error messages you are used to. If you are not used to the messages, "cannot convert" doesn't really mean anything either, as you can't see any conversion in your code.

14. You use srand once at the top of your program to generate the rest of the random numbers. So you use one arbitrary number to help the computer come up with a whole bunch more "random" numbers.
Code:
```//This program will simulate 100 times
//of coin-tossings. Then it will calculate
//the frequency how many times it's HEAD or TAIL
#include <iostream>
#include <iomanip>
#include <cstdlib>
using namespace std;
int flip(void); //return 0 for T(tail=0) and H(head=1)

int main()
{
srand( time(0) );//do once up top
int tailCount=0;
for (int i=1; i<=100; i++) //simulate flipping 100 times
{
switch(flip())
{
case (1):
cout << " " << "H";
break;
case (0):
cout << " " << "T";
tailCount++;
break;
}
if (i%10==0)
cout << endl;
}
cout << "Tail " << tailCount << endl;

return 0;
}
//Function that does flipping the coin
int flip(void)
{
int outcome;
outcome=rand()%2;
return outcome;
}```

Page 1 of 6 123456 Last