Here is a very easy way to test the validity of an integer range function instead of dividing the range into equal units and counting how many 'hits' the units get looking for a reasonable balance. If we have a uniform distribution then the average will be equal to (first + last)/2 for an infinite test but fairly close with a 'decent' size test.
Code: Select all
Function rnd_range (first As Longint, last As Longint) As LongInt
Function = Rnd*(last - first + 1) - .5 + first
End Function
Randomize
Dim As Ulong i
Dim As Longint tot
For i = 1 To 10^8
tot += rnd_range(-123,456)
Next
Print tot/10^8
Sleep
Examples:
Code: Select all
(-5,5) => 0 Progamatically I got 1.118e-005
(-10,-1) => -5.5 -do- I got -5.49982073
(0,9) => 4.5 -do- I got 4.49988167
(1,10) => 5.5 -do- I got 5.50016871
(347,692) => 519.5 -do- I got 519.50917841
(-123,456) => 166.5 -do- I got 166.48569356
That very quickly proved that '-(first>=0)' was actually wrong.
@dodicat
Still not entirely sure what you mean but will this do? It is simply the above adapted to take a random 64-bit value as a further parameter.
Code: Select all
Function rnd_range (RandomNumber as UlongInt, first As Longint, last As Longint) As LongInt
Function = RandomNumber/2^64*(last - first + 1) - .5 + first
End Function