K
Kai-Uwe Bux
Chris said:Kai-Uwe Bux said:Chris said:Hello all,
why you are going far away?
first use srand() to randomize radom number, srand get a parameter as
seed, like:
srand(3); or you can use srand(time(0));
then use this method to have randome number in a certain range:
x=minum number +rand()% maximum number;
Yes, that surely works but brings back my original comment. Using the
modulo operation you interfere with the randomness
How, and why?
and don't make use of the full period of the random generator.
Huh? I see that that can happen. However, I do not see why the
alternative of chopping [0,RAND_MAX] into n intervals of equal length it
a priory better.
Well we're getting into a hot topic here which has been discussed
extensively. On one hand using modulo you might skew the results towards
lower values
No mapping of [0,RAND_MAX+1) to [0,n) can produce truly uniform results
unless n divides RAND_MAX+1. To give correct probabilities you need to
throw away some results from rand().
and on the other hand you can have an effect on the
randomness. The reason is that the modulo operation uses low order bits
and depending on the type of random generator the high-order bits might
show better randomness. However, this is something which depends on the
implementation of rand
That's my point exactly. I do not oppose the statement that there are rand
implementations where modding is significantly worse than rescaling.
and is the subject of endless discussions! For a
thorough discussion on this including the mathematics you might refer to
Knuth's book The Art of computer programming Vol.2 and/or Numerical
Recipes.
Yes and no. Of course you will never get good results if your random
generator is crap. However, the simple scaling and offsetting does not
interfere with any property of the RNG and therefore does not affect the
"randomness".
As a general statement, this seems false: simple scaling gives preference to
high order bits. If the rand() implementation uses a random number
generator whose higher order bits are less random than its lower order
bits, rescaling is worse than taking remainders. (Rumor has it, though,
that such weakness is less common among random number generators than crapy
low-order bits
[snip]In opposition to this the modulo mapping can affect the randomness because
you're introducing a limitation.
Best
Kai-Uwe Bux