Jump to content
Sign in to follow this  
Pured

Unique Random Integers

Recommended Posts

Pured

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

Instead of (obviously more randomised):

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

 

Thank you in advance.

Share this post


Link to post
Share on other sites
Danp2

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 this post


Link to post
Share on other sites
Pured

@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 this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
Sign in to follow this  

×

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.