Sign in to follow this  
Followers 0
adzip2001

Five unique random numbers?

7 posts in this topic

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?

Share this post


Link to post
Share on other sites



#2 ·  Posted (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 by Piano_Man

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

Share this post


Link to post
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 this post


Link to post
Share on other sites

#4 ·  Posted (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 by herewasplato

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

Share this post


Link to post
Share on other sites

#5 ·  Posted (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?...

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 this post


Link to post
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 this post


Link to post
Share on other sites

It is a great community - hang around.

-MSP-


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

Share this post


Link to post
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
Sign in to follow this  
Followers 0