# Issue with Random class, or bad code?

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.

try for starters to use RANDOMIZESEED
not SEED … plus normally you only need (should) seed the random one… unless you wish to recreate a previous sequence

and without spending a lot of time attempting to figure out what you code does… remember this. You say you are using a limited range (1 to 50) and you are taking 7 values… but then you are being conditional on what values you keep, and creating various new random numbers until the pattern matches a criteria. it could be that the logic is determining what you are seeing.

If you where to just take a pure stream of random numbers from 1 to 50, I doubt very much if you would see repeating patterns outside of those due to statistical probability

Thank you Dave. Regarding the code to keep only non repeating values, this is one of the first things I suspected of being bad. I don’t think that it is the cause: here is what I get without the filter (10 columns, a sample within a series of 200 sequences:

34 34 34 34 34 34 34 34 34 34
13 13 13 13 13 13 13 13 13 13
40 40 40 40 40 40 40 40 40 40
18 18 18 18 18 18 18 18 18 18
46 46 46 46 46 46 46 46 46 46
24 24 24 24 24 24 24 24 24 24
3 3 3 3 3 3 3 3 3 3
30 30 30 30 30 30 30 30 30 30
30 30 30 30 30 30 30 30 9 9

Now, I moved the seed outside the loop as you suggested and used R.RandomizeSeed once, instead of using R.Seed in the loop. The filter for non-repeating values is still off. The sample looks a lot better! I will analyse further, but your solution appear to be spot on!. Thanks again!

27 13 45 41 12 45 14 6 17 6
13 30 29 38 40 45 36 9 48 38
45 16 28 4 17 36 3 49 18 29
2 42 20 32 27 18 29 25 38 44
11 17 29 10 39 27 18 14 49 9
7 44 39 5 33 36 4 39 42 41
9 37 39 31 1 32 10 27 7 31
19 13 24 40 19 29 18 45 13 26
26 34 41 35 25 30 14 1 4 43
35 27 35 23 12 42 43 25 34 12
18 49 19 31 6 45 14 42 11 6
19 44 21 6 46 32 8 37 2 2
5 47 33 10 44 18 29 22 15 40
25 36 5 4 23 5 10 5 19 12
22 27 45 40 12 17 14 45 3 40
41 42 46 44 27 36 31 49 28 1
16 6 31 17 22 26 48 3 12 16
35 46 18 45 41 31 43 10 23 19
21 11 2 44 41 36 13 49 31 30
9 10 25 31 12 29 1 26 25 27
39 6 40 36 5 1 41 23 16 25
48 17 38 49 2 45 36 22 35 35
16 46 6 15 39 20 32 11 11 37
12 39 28 33 24 11 15 15 7 29

Random is not random. It’s pseudo-random. One of the properties of a Random sequence, in almost all programming languages, is that for a particular seed value, it is guaranteed to produce the same results. This is very important to programmers. It is also why you should never use it for cryptography.