Woops, forgot <ctime>. But why not use this form of random number generator? Is there something wrong with it? It's quite simple to understand although I'm not sure of it's portability.
Woops, forgot <ctime>. But why not use this form of random number generator? Is there something wrong with it? It's quite simple to understand although I'm not sure of it's portability.
It's portable.
What is "wrong" is very simple as long as you don't go too deep into it: rand as implemented by most C runtime libraries doesn't produce a very good number sequence, meaning that it doesn't appear very random.
All the buzzt!
CornedBee
"There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
- Flon's Law
tu_user,
Imagine that you have already flipped the coin 99 times. Your results are 50 tales and 49 heads. If you can predict that the next flip will be heads... well, that's not random! Each real coin-flip is an independent random event. The coin doesn't know about or care about it's previous flips!
rand() is usually good-enough - As long as you (or the user) can't predict the results, your program will be "random enough". Don't worry that there is an underlying deterministic algorithm.
Unpredictable - The results are not known in advance.
Unbiased - All results are equally likely. You should not be able to say "I usually get more heads than tails".
Often, you do want some bias. For example if you are simulating people's weight, a result of 150 lbs should be more likely than 300 lbs.
Yeah right DougDbug. Thanku. I got it now. Actually I am new to c++ so it will take some time, know what I mean? Anyways I have finally arrived to a conclusion that there is nothin called perfect randomness.