# Five unique random numbers?

## Recommended Posts

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

##### Share on other sites

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

```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```

EDIT: Welcome to the forums!

Edited by Piano_Man

My Programs[list][*]Knight Media Player[*]Multiple Desktops[*]Daily Comics[*]Journal[/list]

##### Share on other sites

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?

##### Share on other sites

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 by herewasplato

[size="1"][font="Arial"].[u].[/u][/font][/size]

##### Share on other sites

...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?...

```Dim \$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)
Next```
Edit - you were close

...and a "Welcome to the forum" from me too...

Edit2: took sleep from 99 down to 9

Edited by herewasplato

[size="1"][font="Arial"].[u].[/u][/font][/size]

##### Share on other sites

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.

##### Share on other sites

It is a great community - hang around.

-MSP-

[size="1"][font="Arial"].[u].[/u][/font][/size]

## Create an account

Register a new account

×

• Wiki

• Back

• Git