This weekend, I created what I thought would be a quick project. The desktop app runs on Windows and needs to generate a list of lists of random numbers within a range. (say, 1000 combinations of 10 numbers within 0 and 50). I expected that within a short range, there would be repeats. What I found however is more akin to patterns, which leads me to wonder whetehr there is something broken with the Random class.
OK: here is the code portion that generates the numbers. I generate the numbers, check for repeating numbers (a requirement is that each sequence cannot have 2 or more members with the same value) and then place the numbers in the array called M(), then copy the array to a listbox for viewing and analysis. Apol,ogies for the use of nondescriptive variables. Here is a legend:
T1: temporary counter for the main loop (values between 6 and 10 depending on user selection)
T2: temporary counter for the sequence check loop (values between 0 and T1-1)
T3 random number temp variable, before it is placed in M()
for T1 = 1 to NM ' Boucle pour la gnration d'une squence
T3 =0
d = Xojo.Core.Date.Now
R.Seed = d.nanosecond
T3 = R.InRange(LI, LS) ' nombre alatoire entre la limite infrieure et la limite suprieure
EqualMember = false
if T1 = 0 then 'Le premier membre est toujours accept
M(T1 - 1) = T3
else
for T2= 0 to T1-1 'chercher un membre gal la nouvelle valeur
if M(T2) = T3 then
EqualMember = true
end if
next
if EqualMember = false then
M(T1 - 1) = T3 'T3 est unique, on l'accepte
else
T1 = T1-1 'T3 n'est pas unique, on recommence
end if
end if
next
Here is a small sample of results, using 7 columns ( the program allows to select 6 to 10 columns) The patterns are clearly visible with larger samples also (more columns and more sequences). In this example, the limits are 1 and 50.
18 46 24 3 30 9 36
36 15 42 20 48 26 5
5 32 11 38 17 44 22
22 1 28 7 34 13 40
40 18 46 24 3 30 9
9 36 15 42 20 48 26
26 5 32 11 38 17 44
44 22 1 28 7 34 13
13 40 18 46 24 3 30
30 9 36 15 42 20 48
48 26 5 32 11 38 17
17 44 22 1 28 7 34
34 13 40 18 46 24 3
3 30 9 36 15 42 20
20 48 26 5 32 11 38
38 17 44 22 1 28 7
7 34 13 40 18 46 24
24 3 30 9 36 15 42
42 20 48 26 5 32 11
11 38 17 44 22 1 28
28 7 34 13 40 18 46
46 24 3 30 9 36 15
15 42 20 48 26 5 32
32 11 38 17 44 22 1
1 28 7 34 13 40 18
18 46 24 3 30 9 36
36 15 42 20 48 26 5
5 32 11 38 17 44 22
22 1 28 7 34 13 40
40 18 46 24 3 30 9
9 36 15 42 20 48 26
26 5 32 11 38 17 44
So here is my interrogation: Is my code at fault, or is there an apparent repeating pattern with the Random class, basically making it unusable where reliable pseudorandom sequences are required? Any insight is welcome.