[SOLVED] Am I Generating Random Numbers Correctly?
I'm not sure what level to pitch this at; I thought "High School" was probably appropriate given that I have not done any proper maths for years! I am a programmer rather than a mathematician, so please forgive me for lapsing into computer-nerd notation rather than using correct maths notation :)
Ok. What I have done is as follows:
I have a set of discrete samples of a measured probability distribution (i.e. it can't be generated by a simple equation, like the gaussian equation, etc). Let's call the array of input values x (e.g. vehicle speed, or whatever) and the associated array of probabilities P. There are N values in each set, such that [i]P is the normalised probability of the value [i]x turning up.
I wanted to write a random number generator that generated values which, over time, have a probability distribution that matches the one I have measured.
So what I did was this:
Pseudo-code (my appologies, I don't know how to write this in the way that a mathematician would!) - this represents the sentiment of what I did, not what I actually did!:
My question is:
BEGIN FUNCTION RandDist():
LET N = a very large integer (e.g. 0x7FFFFFFF).
LET xmin be the minimum value of x
LET xmax be the maximum value of x
LET random(a, b) be a function which returns different a random number
each time it is called, between a and b inclusive, which has a flat
LET P(x) be a function which returns the measured probability of x (or
nearest measured value to x) turning up.
LET r be the results array.
LOOP N times:
LET v = random(xmin, xmax)
LET w = random(0, 1)
IF w < P(v):
break out of while loop
append v to r
Do you think this is sensible?? I know there can be problems regarding random number generators not returning truly uncorrelated values. I am also concerned about using the same generator to provide values for v and w.
A penny for your thoughts ...