Jump to content

[SOLVED] Random number exclusion?


Recommended Posts

How do I select a random number between a min and a max, and then after picking that number select another random number between the same min/max excluding the first number I picked?

Example: I want to select 5 random numbers between 1 and 12, and each selection can not be the same. If I select the number 6 the first time I do not want it to be possible to select the number 6 again.

The only way I can see it being done is to compare the later results with the former results and if they are the same, run the random number generator again.. but then is it still truely random? I am no mathmatician so I have no idea... but it does need to be completely random. Does picking an already picked number, tossing it back, and selecting again count as random?

Edited by SkybotGaming
Link to comment
Share on other sites

If you require the full range of numbers between a max and min value to be arranged in random order then you could also do something like this:

#include <Array.au3>

_Example()

Func _Example()
    Local $aArray[100]
    For $i = 0 to 99
        $aArray[$i] = $i
    Next

    _ArrayShuffle($aArray)
    _ArrayDisplay($aArray)
EndFunc

Func _ArrayShuffle(ByRef $aArray)
    If IsArray($aArray) = 0 Or UBound($aArray, 0) > 1 Then Return SetError (1)

    Local $vTemp, $r, $iBound = UBound($aArray) -1
    For $i = 0 To $iBound
        $r = Random(0, $iBound, 1)
        $vTemp = $aArray[$i]
        $aArray[$i] = $aArray[$r]
        $aArray[$r] = $vTemp
    Next
EndFunc
Edited by czardas
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...