On 06/06/14 20:39, meino.cra...@gmx.de wrote:
To get an better imagination of that...suppose the rand() would only
return numbers in the range of 1...12 and the alphabet has only 8
characters (as 2^32 is not devideable by 62)
rand():
1 2 3 4 5 6 7 8 9 10 11 12
rand()%N : rand()%7
1 2 3 4 5 6 7 0 1 2 3 4
or in other words: An even distribution of numbers of rand()
would result in a unevenly distributed sequence of characters...or?
This would break the quality of ISAACs output.
I am sure I did something wrong here...but where is the logic trap?
You thought on that is totally right.
Let's say random numbers have the rande [0..RAND_MAX]
Blindly calculating modulus
creates a bias towards lower numbers if RAND_MAX+1 is not divisible
by N. For most applications this bias is negligible. If you really
want the same distribution you have to discard numbers greater than
RAND_MAX - ((RAND_MAX+1)%N)
(Beware the undefined behaviour in the above line if you try C.)
Basically you want one less than the largest by N dividable number
in your range.
With numbers:
rand():
0 1 2 3 4 5 6 7 8 9 10 11 12
rand()%5 with discarding >12-((12+1)%5)=9:
0 1 2 3 4 0 1 2 3 4 discarded
---
null_ptr
Please don't follow me.