Jump to content

Five unique random numbers?


Recommended Posts

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

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 by Piano_Man
My Programs[list][*]Knight Media Player[*]Multiple Desktops[*]Daily Comics[*]Journal[/list]
Link to comment
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?

Link to comment
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]

Link to comment
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]

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