Jump to content

I need to call a different random number several times


Recommended Posts

czardas,

Not my intention to confuse.  I just wanted to see the script response to an impossible request.  I did write some code to make your code user friendly to someone wanting to generate perhaps lottery tickets.  This works for me:

#include <Array.au3>

$num = InputBox ("Number of Integers", "How many unique random numbers do you want to generate?")
$min = InputBox ("Minimum number", "What is the minimum random number to generate?")
$max = InputBox ("Maximum number", "What is the maximum random number to generate?")
If $max - $min +1 < $num Then
    MsgBox (1, "Impossible", "One cannot generate " & $num & " unique numbers between " & $min & " and " & $max)
    Exit
EndIf

Global $gaRandom = _RandomToArray($num, $min, $max); _RandomToArray(number of generated integers, minimum integer, maximum integer)

_ArrayDisplay($gaRandom)

; Access the array elements.
For $i = 0 to UBound($gaRandom) -1
    ConsoleWrite($gaRandom[$i] & @CRLF)
Next

Func _RandomToArray($iCount, $iMin, $iMax)

    If $iCount < 1 Or $iMin > $iMax Then Return SetError(1, 0, 0)
    Local $iBound = $iMax - $iMin + 1
    If $iBound < $iCount Then Return SetError(2, 0, 0)

    Local $aArray[$iBound]
    For $i = 0 to $iBound - 1
        $aArray[$i] = $iMin + $i
    Next
    _ArrayShuffle($aArray)
    ReDim $aArray[$iCount]

    Return $aArray
EndFunc

Again, many thanks.  I love the  fact that I can come to this forum with a question and get it answered, or just to look around at the threads and pick up good ideas for current or future use.

_aleph_

Meds.  They're not just for breakfast anymore. :'(

Link to comment
Share on other sites

aleph01 - Great! :) I'm happy if this is useful to you. Just one thing though, I wrote this as a small example of how to tackle this kind of problem, and it only worked with numbers. The value returned from an input control is always a string. I don't know the reasons, but negative numeric strings are not converted automatically, so you have to use Number() to make the conversion correctly. I have modified the function to accept negative numeric string input from the InputBox. You can see the two new lines added to the function.

I also added two illustrative methods to check for errors.

;

#include <Array.au3>

Global $num, $min, $max ; Declare variables

$num = InputBox ("Number of Integers", "How many unique random numbers do you want to generate?")
If @error Then Exit ; Check if the user clicked cancel.
$min = InputBox ("Minimum number", "What is the minimum random number to generate?")
If @error Then Exit
$max = InputBox ("Maximum number", "What is the maximum random number to generate?")
If @error Then Exit

Global $gaRandom = _RandomToArray($num, $min, $max)
Switch @error
    Case 1
        MsgBox (0, "Error", "One, or more, of the numbers you entered is out of range.")
        Exit
    Case 2
        MsgBox (0, "Impossible", "One cannot generate " & $num & " unique numbers between " & $min & " and " & $max)
        Exit
EndSwitch

_ArrayDisplay($gaRandom)

; Access the array elements.
For $i = 0 to UBound($gaRandom) -1
    ConsoleWrite($gaRandom[$i] & @CRLF)
Next

Func _RandomToArray($iCount, $iMin, $iMax)
    $iMin = Number($iMin) ; In case negative numbers are passed as strings.
    $iMax = Number($iMax) ; As above.

    If $iCount < 1 Or $iMin > $iMax Then Return SetError(1, 0, 0)
    Local $iBound = $iMax - $iMin + 1
    If $iBound < $iCount Then Return SetError(2, 0, 0)

    Local $aArray[$iBound]
    For $i = 0 to $iBound - 1
        $aArray[$i] = $iMin + $i
    Next
    _ArrayShuffle($aArray)
    ReDim $aArray[$iCount]

    Return $aArray
EndFunc
Edited by czardas
Link to comment
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
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...