Jump to content

Random number only used once


Recommended Posts

I'm thinking of making a test engine to replace the test engine I'm using for my AutoIt quiz. I like to have a randomizer so if I have a number of questions, I can make the order of the questions random. I looked around, and couldn't quite find what I needed. I'm not sure on how to go about it. Any advice?

Link to comment
Share on other sites

Random.....

#Include <Array.au3>
Global $MaxRandom = 10
Global $Randoms[1]
$Randoms[0] = Random(0, $MaxRandom, 1)
Global $NewRandom
Global $Track = 0
For $Track = 0 To ($MaxRandom - 1) Step 1
    Do
    $NewRandom = Random(0, $MaxRandom, 1)
    Until _ArraySearch($Randoms, $NewRandom) = -1
    _ArrayAdd($Randoms, $NewRandom)
Next
_ArrayDisplay($Randoms, "Randoms")

Here you go.

Edited by AutoIt Smith
Link to comment
Share on other sites

  • Moderators

Here's another idea based on this thread:

http://www.autoitscript.com/forum/index.ph...c=22564&hl=

;Let's say you want 10 unique numbers out of 1000
Local $HowManyUnique = 10, $NumberToStartAt = 0, $NumberOfChoices = 1000, $ZeroForDecimalOneForNone = 1

Local $UniqueRandomNumbers = _RandomNumDisplay($HowManyUnique, $NumberToStartAt, $NumberOfChoices, $ZeroForDecimalOneForNone)
Local $sArrayDisplay

For $i = 1 To UBound($UniqueRandomNumbers) - 1
    $sArrayDisplay &= $UniqueRandomNumbers[$i] & @LF
Next
MsgBox(64, 'Info:', StringTrimRight($sArrayDisplay, 1))
Func _RandomNumDisplay($iCount, $iMin = 0, $iMax = 1, $iFlag = 0)
    Local $Rand_Array = '', $StartCount = 1
    If $iCount > ($iMax - ($iMin - 1)) And $iFlag = 1 Then Return SetError(1, 0, 0)
    If ($iMin >= $iMax) Then Return SetError(2, 0, 0)
    If Not IsNumber($iCount) Or Not IsNumber($iMin) Or Not IsNumber($iMax) Or Not IsNumber($iFlag) Then SetError(3, 0, 0)
    Do
        $Rand_Number = Random($iMin, $iMax, $iFlag)
        If Not StringInStr(Chr(01) & $Rand_Array, Chr(01) & $Rand_Number & Chr(01)) Then
            $StartCount += 1
            $Rand_Array &= $Rand_Number & Chr(01)
        EndIf
    Until $StartCount = $iCount
    Return StringSplit(StringTrimRight($Rand_Array, 1), Chr(01))
EndFunc
Edit:

Showed the wrong function, the one I edited for him here was a tad faster with the Do/Until loop.

http://www.autoitscript.com/forum/index.ph...st&p=156765

Edit:

Fixed flawed logic in Do/Until loop

Edited by SmOke_N

Common sense plays a role in the basics of understanding AutoIt... If you're lacking in that, do us all a favor, and step away from the computer.

Link to comment
Share on other sites

Here's another idea. Slightly faster (not that it matters with 10 elements) and probably a little better scalability. Also works with production release.

#Include <Array.au3>
Global $MaxRandom = 10
Global $Randoms[$MaxRandom]
For $i = 0 to $MaxRandom - 1
    $Randoms[$i] = $i + 1     ; Define the numbers
Next
For $j = 1 to 3     ; Modify stop number to adjust "mixiness"
    For $i = 0 to $MaxRandom - 1     ; Now mix them up
        $RandomTmp1 = $Randoms[$i]
        $i2 = Random(0, $MaxRandom - 1, 1)
        $Randoms[$i] = $Randoms[$i2]
        $Randoms[$i2] = $RandomTmp1
    Next
Next
_ArrayDisplay($Randoms, "Randoms")
BlueBearrOddly enough, this is what I do for fun.
Link to comment
Share on other sites

  • Moderators

Here's another idea. Slightly faster (not that it matters with 10 elements) and probably a little better scalability. Also works with production release.

#Include <Array.au3>
Global $MaxRandom = 10
Global $Randoms[$MaxRandom]
For $i = 0 to $MaxRandom - 1
    $Randoms[$i] = $i + 1     ; Define the numbers
Next
For $j = 1 to 3     ; Modify stop number to adjust "mixiness"
    For $i = 0 to $MaxRandom - 1     ; Now mix them up
        $RandomTmp1 = $Randoms[$i]
        $i2 = Random(0, $MaxRandom - 1, 1)
        $Randoms[$i] = $Randoms[$i2]
        $Randoms[$i2] = $RandomTmp1
    Next
Next
_ArrayDisplay($Randoms, "Randoms")

Edit:

Actually the larger the Random number got, the faster yours became over mine :D

Edit2:

Still have to give kudos to greenmachines idea, if you only want to pull 50 random numbers from 10000 of them, the RandomNumDisplay is still a nice choice. But if your generating just x numbers from x set equally, I'd go with Blues.

Edited by SmOke_N

Common sense plays a role in the basics of understanding AutoIt... If you're lacking in that, do us all a favor, and step away from the computer.

Link to comment
Share on other sites

well.... heres my Lotto "Quick-Pick"

Func Randomize()
    $R[1] = Random(1, 47, 1)
    $R[2] = Random(1, 47, 1)
    $R[3] = Random(1, 47, 1)
    $R[4] = Random(1, 47, 1)
    $R[5] = Random(1, 47, 1)
    $R[6] = Random(1, 27, 1)
    If $R[1] = $R[2] Or $R[1] = $R[3] Or $R[1] = $R[4] Or $R[1] = $R[5] Then Randomize()
    If $R[2] = $R[3] Or $R[2] = $R[4] Or $R[2] = $R[5] Then Randomize()
    If $R[3] = $R[4] Or $R[3] = $R[5] Then Randomize()
    If $R[4] = $R[5] Then Randomize()
    _ArraySort($R, 0, 1, 5)
    GUICtrlSetData($QWinner, "Quick Pick =  " & $R[1] & " " & $R[2] & " " & $R[3] & " " & $R[4] & " " & $R[5] & @CRLF & "Mega = " & $R[6] & " ")
EndFunc  ;==>Randomize

8)

NEWHeader1.png

Link to comment
Share on other sites

  • Moderators

well.... heres my Lotto "Quick-Pick"

Func Randomize()
    $R[1] = Random(1, 47, 1)
    $R[2] = Random(1, 47, 1)
    $R[3] = Random(1, 47, 1)
    $R[4] = Random(1, 47, 1)
    $R[5] = Random(1, 47, 1)
    $R[6] = Random(1, 27, 1)
    If $R[1] = $R[2] Or $R[1] = $R[3] Or $R[1] = $R[4] Or $R[1] = $R[5] Then Randomize()
    If $R[2] = $R[3] Or $R[2] = $R[4] Or $R[2] = $R[5] Then Randomize()
    If $R[3] = $R[4] Or $R[3] = $R[5] Then Randomize()
    If $R[4] = $R[5] Then Randomize()
    _ArraySort($R, 0, 1, 5)
    GUICtrlSetData($QWinner, "Quick Pick =  " & $R[1] & " " & $R[2] & " " & $R[3] & " " & $R[4] & " " & $R[5] & @CRLF & "Mega = " & $R[6] & " ")
EndFunc ;==>Randomize

8)

Never run int a recursion issue there? (Maybe not since your not randomizing but a few numbers...)

Common sense plays a role in the basics of understanding AutoIt... If you're lacking in that, do us all a favor, and step away from the computer.

Link to comment
Share on other sites

I'm looking to have a test bank of questions that could be up to 400 questions. I'll try this out and see how it does. I'm looking to see if I can design it so I can use my current test file format so I don't have to redo all my test. (I've got WAY to many questions in my test catalog. My Net + stuff is in the hundreds, and redoing that would be a real pill.)

Edited by vollyman
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...