# Unique Random Integers

## Recommended Posts

Hello,

I've written this simple function many times before. All it does is simply prints sets of 16 unique numbers (4x4).

Now, I don't know if my problem is something I'm missing or a different in AutoIT (doubt it). I've been looking through the code for 30 minutes now. I don't see the flaw. Could you please point out the issue?

```Global \$array[16] = [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]
Global \$number = 0
Global \$found = False
Global \$i = 0

\$file = FileOpen("RandomNumbers.txt", 1) ;Open a .txt file.

For \$setsDone = 1 To 5 ;How many levels.
For \$arraySize = 0 To 15 ;16 tiles per level.
If(\$arraySize = 0) Then ;First number.
\$array[0] = \$number
Else ;Not first number.
\$temp = \$arraySize

While(\$i < \$arraySize)
\$number = Random(1, 16, 1) ;Get a random integer.

For \$x = 0 To \$i - 1 ;Check if the number exists in the array.
If(\$array[\$x] = \$number) Then
\$found = True ;Set the flag to true (number is already in the array.
EndIf
Next

If(\$found = False) Then ;If the number is not in the array.
\$array[\$i] = \$number
EndIf

\$found = False ;Reset flag.

\$i = \$i + 1 ;Look for the next number.
WEnd

\$i = 0 ;Reset \$i for the next loop.
EndIf
Next

For \$loop1 = 0 To 3
For \$loop2 = 0 To 3
FileWrite(\$file, \$array[(\$loop1 * 4) + \$loop2] & " ") ;Log the numbers to the file.
Next

FileWrite(\$file, @CRLF) ;Enter a newline in the file.
Next

FileWrite(\$file, @CRLF) ;Enter a newline in the file.

For \$x = 0 to 15 ;Clear the array.
\$array[\$x] = 0
Next
Next

FileClose(\$file) ;Close the .txt file.```

You can run the script as is. You may see results like:

1 9 2 5
11 14 15 7
13 13 1 6
6 8 10 0

1 2 3 4
8 7 6 5
9 10 11 12
16 15 14 13

##### Share on other sites

When I ran your code, the numbers appeared random, but not unique. Here's a rewrite that appears to work correctly to me --

```#include <Array.au3>

Local \$setsDone, \$i, \$loop1, \$loop2
Local \$file = FileOpen("RandomNumbers.txt", 1) ;Open a .txt file.

For \$setsDone = 1 To 5 ;How many levels.
Global \$array[16] = [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]

For \$i = 0 To 15 ;16 tiles per level.
\$array[\$i] = GetRandom(1, 16, \$array)
Next

For \$loop1 = 0 To 3
For \$loop2 = 0 To 3
FileWrite(\$file, \$array[(\$loop1 * 4) + \$loop2] & " ") ;Log the numbers to the file.
Next

FileWrite(\$file, @CRLF) ;Enter a newline in the file.
Next

FileWrite(\$file, @CRLF) ;Enter a newline in the file.
Next

FileClose(\$file) ;Close the .txt file.

Func GetRandom(\$pMin, \$pMax, \$pArray)
Local \$iRandom

While True
\$iRandom = Random(1, 16, 1) ;Get a random integer.

If _ArraySearch(\$pArray, \$iRandom) = -1 Then
ExitLoop
EndIf
WEnd

Return \$iRandom
EndFunc```

##### Share on other sites

@Dabo, Thanks, that code works. Mine seemed to be doing the same thing, expect I used the \$found = True instead of a function returning -1.

Do you know what the error in my logic was?

##### Share on other sites

No, but I would guess that it is within the While loop. My thinking is that you should be using \$arraySize as the index to the array, but instead you are using \$i as the array index.

## Create an account

Register a new account