adzip2001 Posted May 6, 2007 Share Posted May 6, 2007 Please help... I am trying to come up with a way to generate five unique integers at a time (1 through 71), meaning every time you run the function, it generates $R1, $R2, $R3, $R4, and $R5, where: $R1 is not equal to $R2, $R3, $R4, and $R5 $R2 is not equal to $R3, $R4, and $R5 $R3 is not equal to $R4, and $R5 $R4 is not equal to $R5 Could someone help me please? Link to comment Share on other sites More sharing options...
Achilles Posted May 6, 2007 Share Posted May 6, 2007 (edited) Please help... I am trying to come up with a way to generate five unique integers at a time (1 through 71), meaning every time you run the function, it generates $R1, $R2, $R3, $R4, and $R5, where: $R1 is not equal to $R2, $R3, $R4, and $R5 $R2 is not equal to $R3, $R4, and $R5 $R3 is not equal to $R4, and $R5 $R4 is not equal to $R5 Could someone help me please?Dim $numbers[5] ;Creates an array with 5 objects For $a = 0 to 4 ;Loops through 5 times Do ;Loop until the number is unequal to any previous numbers $var = Random(1,71,1) ;Generate a random integer between 1 and 71 Until $var <> $numbers[0] and $var <> $numbers[1] and $var <> $numbers[2] and $var <> $numbers[3] and $var <> $numbers[4] $numbers[$a] = $var ;Adds the random number to the array because it is unique to the rest of the array Next For $a = 0 to 4 ;Loops through 5 times Msgbox(0, 'Number: ' & $a + 1, $numbers[$a]) ;Show the numbers one by one Next Please ask if you don't understand anything! EDIT: Welcome to the forums! Edited May 6, 2007 by Piano_Man My Programs[list][*]Knight Media Player[*]Multiple Desktops[*]Daily Comics[*]Journal[/list] Link to comment Share on other sites More sharing options...
adzip2001 Posted May 6, 2007 Author Share Posted May 6, 2007 Absolutely ingenious! I wouldn't have even known to use "Until", or the rest of it for that matter. Thank you very, very much. A quick follow-up question; how can I take those integers, and use them to make other variables? In other words, if the array now constists of 2, 43, 11, 10, and 30, how can I use those numbers to refer to the corresponding variables, i.e. $Question2, $Question43, $Question11, $Question10, and $Question30? I was thinking $Question$numbers[1], $Question$numbers[2], $Question$numbers[3], and so forth, but is that even the right syntax? Link to comment Share on other sites More sharing options...
herewasplato Posted May 6, 2007 Share Posted May 6, 2007 (edited) It took me a bit longer than Piano_Man to code a solution to your first question. This code allows one to easily change the number of unique numbers generated (or even generate a random number of random numbers)$min = 1 $max = 71 $numbers = 5 ;gen first random number $output = Random($min, $max, 1) ;gen second and subsequent random numbers, if any For $i = 2 To $numbers $outputArray = StringSplit($output, ",") $temp = Random($min, $max, 1) $unique = "yes" ;check for uniqueness For $ii = 1 To $outputArray[0] Sleep(9) If $temp = $outputArray[$ii] Then $unique = "no" Next If $unique = "yes" Then ;add to the unique number to the string $output = $output & "," & $temp Else ;decrement "i" and try again for a unique number $i = $i - 1 EndIf Next ConsoleWrite($output)Edit: took sleep from 99 down to 9 Edited May 6, 2007 by herewasplato [size="1"][font="Arial"].[u].[/u][/font][/size] Link to comment Share on other sites More sharing options...
herewasplato Posted May 6, 2007 Share Posted May 6, 2007 (edited) ...In other words, if the array now constists of 2, 43, 11, 10, and 30, how can I use those numbers to refer to the corresponding variables, i.e. $Question2, $Question43, $Question11, $Question10, and $Question30?...expandcollapse popupDim $Questions[100] $Questions[1] = "Question 1" $Questions[2] = "Question 2" $Questions[3] = "Question 3" $Questions[4] = "Question 4" $Questions[5] = "Question 5" $Questions[6] = "Question 6" $Questions[7] = "Question 7" $min = 1 $max = 7 $numbers = 5 ;gen first random number $output = Random($min, $max, 1) ;gen second and subsequent random numbers, if any For $i = 2 To $numbers $outputArray = StringSplit($output, ",") $temp = Random($min, $max, 1) $unique = "yes" ;check for uniqueness For $ii = 1 To $outputArray[0] Sleep(9) If $temp = $outputArray[$ii] Then $unique = "no" Next If $unique = "yes" Then ;add to the unique number to the string $output = $output & "," & $temp Else ;decrement "i" and try again for a unique number $i = $i - 1 EndIf Next ConsoleWrite($output & @CRLF & @CRLF) $outputArray = StringSplit($output, ",") For $i = 1 To $outputArray[0] ConsoleWrite($Questions[$outputArray[$i]] & @CRLF) NextEdit - you were close ...and a "Welcome to the forum" from me too... Edit2: took sleep from 99 down to 9 Edited May 6, 2007 by herewasplato [size="1"][font="Arial"].[u].[/u][/font][/size] Link to comment Share on other sites More sharing options...
adzip2001 Posted May 6, 2007 Author Share Posted May 6, 2007 You guys are the best. Thank you for being so willing and able to help. You were a great help, I am done with my program! Thanks again. Link to comment Share on other sites More sharing options...
herewasplato Posted May 6, 2007 Share Posted May 6, 2007 It is a great community - hang around. -MSP- [size="1"][font="Arial"].[u].[/u][/font][/size] 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