PDA

View Full Version : The Limit of Randomize()

danbaron
18-02-2010, 09:34
Here are the results of my testing of the thinBasic function, Randomize().

DTB

Uses "CONSOLE"

Global V1, V2 As Ext

Function TBMAIN()

I = 1

Do
I -= 1
Randomize(I)
V1 = Rnd
Randomize(I - 1)
V2 = Rnd
If V1 = V2 Then Exit Do
Loop

Console_WriteLine(I)

I = -1

Do
I += 1
Randomize(I)
V1 = Rnd
Randomize(I + 1)
V2 = Rnd
If V1 = V2 Then Exit Do
Loop

Console_WriteLine(I)

WaitKey
End Function

'(The program assumes that for two seed values, I and J, that if the
'the first value of Rnd for Randomize(I), equals the first value
'of Rnd for Randomize(J), then the two pseudo-random sequences are
'identical, and the seeds I and J are effectively the same.)

'The output from the program is:

'-16777216
' 16777216

'Apparently, Randomize() has 16,777,216 possible different seeds.

'(The negative seeds are correlated to the positive seeds.
'For any number, I, the first value of Rnd in the sequence for
'Randomize(-I), equals the first value of Rnd in the sequence for
'Randomize(I), plus or minus 0.5. Whether plus or minus is used in
'a particular case, is decided by requiring that both values be
'in the interval, 0 < value < 1.)

'16,777,216 equals 2^24.

'So, I think, no matter how big the seed is, only 24 bits are used.

'For seeds greater than 16,777,216, the sequences begin to repeat.
'For instance, the sequence for 16,777,217 is the same as the sequence
'for 16,777,216.

'As the sizes of seeds continue to increase, the number of consecutive
'seeds with the same sequence, increases. For instance, the sequences
'for seeds, 1,677,721,665 - 1,677,721,791 are identical.

'Therefore, I think that for seeds with more than 24 bits, the 24 high
'significant bits are used.

'Randomize() also works with floating point seeds. But, in that case, I
'think that again, only 24 bits are used.

'I forgot to mention that Randomize(0) seems to be a special case. The
'first value in its sequence, is 0.