I am really confused on how the use of a random number generator works when using cutoff and being unbiased. Here is the code that I was given to incorporate into my rng and I am particuarly confused with what the p value does I would greatly appreciate any help.
long randint(long low, long high)
{
int s,MAX, cutoff
s=MAX/(n+1);
cutoff=s*(n+1);
while((r=rng()) >= cutoff)
;
p=r/s;
}
This is the puedocode the namespace and include was already included in the program.
1 is returned 3/10 (30%) of the time;
2 is returned 4/10 (40%) of the time;
3 is returned 3/10 (30%) of the time.
Therefore the algorithm is biased towards 2.
Note that this happens because the last rng() value -- 10
yields the fourth 2 result.
If the rng() is zero-based, (sorry, my example isn't), then
rng() results 0...N-1 (where N is the width of the range of
numbers you want) yield an unbiased generator for your
range. That is, every rng() result maps to exactly one
value in your range. Using the modulo trick (rng() % N)
means that rng() results 0...k*N-1 is also unbiased.
The bias gets introduced when rng() can return fewer
than N values greater than k*N-1. To eliminate the bias,
you discard all rng() results greater than k*N-1. This
is the purpose of the above while() loop.
Well, first you have to post syntactically correct code, otherwise I can't be
sure of the real intention of the function or if there are any other typos.
Aside from the missing semicolon on the first line of the function, MAX is
used but uninitialized; n, r, and p are used but undeclared, and the function
doesn't actually return anything.