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

##### Share on other sites

Random.....

```#Include <Array.au3>
Global \$MaxRandom = 10
Global \$Randoms
\$Randoms = 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
Next
_ArrayDisplay(\$Randoms, "Randoms")```

Here you go.

Edited by AutoIt Smith

##### Share on other sites

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.

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

##### 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")```

Edit:

Actually the larger the Random number got, the faster yours became over mine 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.

##### Share on other sites

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

```Func Randomize()
\$R = Random(1, 47, 1)
\$R = Random(1, 47, 1)
\$R = Random(1, 47, 1)
\$R = Random(1, 47, 1)
\$R = Random(1, 47, 1)
\$R = Random(1, 27, 1)
If \$R = \$R Or \$R = \$R Or \$R = \$R Or \$R = \$R Then Randomize()
If \$R = \$R Or \$R = \$R Or \$R = \$R Then Randomize()
If \$R = \$R Or \$R = \$R Then Randomize()
If \$R = \$R Then Randomize()
_ArraySort(\$R, 0, 1, 5)
GUICtrlSetData(\$QWinner, "Quick Pick =  " & \$R & " " & \$R & " " & \$R & " " & \$R & " " & \$R & @CRLF & "Mega = " & \$R & " ")
EndFunc  ;==>Randomize```

8) ##### Share on other sites

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

```Func Randomize()
\$R = Random(1, 47, 1)
\$R = Random(1, 47, 1)
\$R = Random(1, 47, 1)
\$R = Random(1, 47, 1)
\$R = Random(1, 47, 1)
\$R = Random(1, 27, 1)
If \$R = \$R Or \$R = \$R Or \$R = \$R Or \$R = \$R Then Randomize()
If \$R = \$R Or \$R = \$R Or \$R = \$R Then Randomize()
If \$R = \$R Or \$R = \$R Then Randomize()
If \$R = \$R Then Randomize()
_ArraySort(\$R, 0, 1, 5)
GUICtrlSetData(\$QWinner, "Quick Pick =  " & \$R & " " & \$R & " " & \$R & " " & \$R & " " & \$R & @CRLF & "Mega = " & \$R & " ")
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.

##### Share on other sites

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

nopper... not yet anyways

i have tested it many, many times

8) ##### 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

## Create an account

Register a new account

• ### Recently Browsing   0 members

×

• Wiki

• Back

• #### Beta

• Git
• FAQ
• Our Picks
×
• Create New...