aleph01 Posted January 31, 2015 Share Posted January 31, 2015 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_ czardas 1 Meds. They're not just for breakfast anymore. Link to comment Share on other sites More sharing options...
czardas Posted January 31, 2015 Share Posted January 31, 2015 (edited) 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. ; expandcollapse popup#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 January 31, 2015 by czardas operator64 ArrayWorkshop Link to comment Share on other sites More sharing options...
Recommended Posts
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 accountSign in
Already have an account? Sign in here.
Sign In Now