Andreik Posted July 7, 2008 Share Posted July 7, 2008 (edited) I have an array with 30 subscripts and I want to assign random numbers between 1 and 15 for each 2 subscripts of array. Example: $Array[1] = 5 $Array[2] = 15 $Array[3] = 2 $Array[4] = 8 $Array[5] = 6 $Array[6] = 13 $Array[7] = 14 $Array[8] = 4 $Array[9] = 8 $Array[10] = 12 $Array[11] = 3 $Array[12] = 14 $Array[13] = 4 $Array[14] = 5 $Array[15] = 6 $Array[16] = 2 $Array[17] = 13 $Array[18] = 7 $Array[19] = 9 $Array[20] = 10 $Array[21] = 7 $Array[22] = 12 $Array[23] = 1 $Array[24] = 3 $Array[25] = 10 $Array[26] = 11 $Array[27] = 9 $Array[28] = 11 $Array[29] = 15 $Array[30] = 1 Any idea how can I do that? Edited July 7, 2008 by Andreik When the words fail... music speaks. Link to comment Share on other sites More sharing options...
TomZ Posted July 7, 2008 Share Posted July 7, 2008 (edited) Dim $array[30] For $i = 0 to UBound($array)-1 $array[$i] = Random(1, 15, 1) Next It can't be that hard? muttley EDIT: Maybe I got you wrong, and you wanted to assign each of the numbers 1-15 to two random elements of the array. If so, please say so. Edited July 7, 2008 by TomZ Link to comment Share on other sites More sharing options...
Andreik Posted July 7, 2008 Author Share Posted July 7, 2008 Dim $array[30] For $i = 0 to UBound($array)-1 $array[$i] = Random(1, 15, 1) Next It can't be that hard? muttley EDIT: Maybe I got you wrong, and you wanted to assign each of the numbers 1-15 to two random elements of the array. If so, please say so.I want to assign no more 2 values. With random is possible to assign more. When the words fail... music speaks. Link to comment Share on other sites More sharing options...
TomZ Posted July 7, 2008 Share Posted July 7, 2008 I see. I quickly wrote this function, it should do what you want. FillArrayRandom(ByRef $array, $min, $max, $maxOccurence, $round = 0, $maxTries = 10000) $array = The array to fill (should be empty) $min = Minimum random number $max = Maximum random number $maxOccurence = How many times a number may be used (0 = no limit) $round = How many decimals should be used (default = 0, none) $maxTries = After how many tries to give up if no suitable number is found $maxTries should be greater, if you want to fill a large array. Sets @error to 1 if $maxTries is exceeded. #include <Array.au3> Dim $randomArray[30] FillArrayRandom($randomArray, 1, 15, 2) _ArrayDisplay($randomArray) Func FillArrayRandom(ByRef $array, $min, $max, $maxOccurence, $round = 0, $maxTries = 10000) Local $i, $number For $i = 0 to UBound($array)-1 For $n = 0 to $maxTries $number = Round(Random($min, $max), $round) If $maxOccurence = 0 OR _KeyInArrayCount($array, $number) < $maxOccurence Then ExitLoop Next If _KeyInArrayCount($array, $number) >= $maxOccurence Then Return SetError(1) $array[$i] = $number Next EndFunc Func _KeyInArrayCount($array, $key) Local $i, $n = 0 For $i = 0 to UBound($array)-1 If $array[$i] = $key Then $n += 1 Next Return $n EndFunc Link to comment Share on other sites More sharing options...
Andreik Posted July 7, 2008 Author Share Posted July 7, 2008 OK. Thanks TomZ! When the words fail... music speaks. 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