Hmm, I am trying to think about this in a short way...
How can I use my random function and still make it random? |
There is nothing wrong with your random function. It is working fine.
To explain, let us look at one of the original pseudo-random number generators:
the John von Neumann’s
Middle Squared method.
[1]
Start by taking a number. We will call this the
seed.
42
To get the next random number, square it, then take the middle two digits of the resulting four digit integer.
422 = 1764
Middle two digits are 76
Answer: 76
There is your first pseudo-random number.
To get the next one, you just plug the number back in:
762 = 5776
Middle two digits are 77
Answer: 77
Von Neumann used significantly larger numbers, but hopefully you should already see that this has some significant problems.
[2] (There are two. Can you figure out what they are?)
Even though the Middle-Squared Method has problems, it is the way that
all PRNGs work. Except for the seed, which the user must supply, each successive generation is computed from the previous generation.
This is the case with that
random() function you were given. The “seed” is initially a value obtained from the clock (via
time(0)). After that, every time you want a new random number you use the last one you got out of it. That is why the
seed argument is a reference.
I don't see how I removed all randomness from the simulation. |
Knowing now how you compute a random number using successive permutations of a value, you must see that you cannot get random numbers without performing this permutation.
[3]
In other words, how often does your code call
random(). The assignment says you must compute a random vector
every time you start, and
every time you have a collision.
If you pre-compute these vectors and reuse them, then all you are doing is sending the
same particle through your simulation a thousand times.
But the whole point of the simulation is to send
different particles through and tally what happens.
Look to see where you are computing randomness.
Hope this helps.
1. This was created back in the 1940s when developing computer simulations on nuclear weapon deployment. Interested people wanted to make sure detonating a bomb wouldn’t destroy all life on earth. (And then they just wanted to know how destructive their shiny new weapons could be.)
2. Von Neumann liked the problems because it became pretty obvious when the simulation failed.
3. Yes, this is a tautology.