# Random Numbers

## Recommended Posts

Hopefully I am not double posting but i could not find any answers in the forums.

I need to generate random numbers to generate random exam papers.

my script will return for example: 1, 4, 5, 2, 1, 7, 4, 10, 5, 15, 12, 2, 14, 3, 5.........

i can not have duplicate numbers generated, that will cause some duplicate questions on the exam paper.

I have 50 questions, need to randomly select only 20 of them.

thanks

Bouval

##### Share on other sites

I don't usually do this... but I saw weaponx typing

```#include <array.au3>;For _ArrayDisplay
\$sRanString = _RandomGenerate(1, 50, 20)
MsgBox(0, "Returned as String", \$sRanString)
\$aRanArray = _RandomGenerate(1, 50, 20, 0)
_ArrayDisplay(\$aRanArray, "Returned as an Array")

Func _RandomGenerate(\$nMinRnd, \$nMaxRnd, \$nMaxOut, \$nAsString = 1)
Local \$sNHold, \$nRand, \$vChar = ",", \$iCC = 0
While \$iCC < \$nMaxOut
\$nRand = Random(\$nMinRnd, \$nMaxRnd, 1)
If StringInStr(\$vChar & \$sNHold, \$vChar & \$nRand & \$vChar) = 0 Then
\$sNHold &= \$nRand & \$vChar
\$iCC += 1
EndIf
WEnd
If \$nAsString Then Return StringTrimRight(\$sNHold, 1)
Return StringSplit(StringTrimRight(\$sNHold, 1), \$vChar)
EndFunc```

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

```#include <Array.au3>

Dim \$array[50]

;Fill array with numbers 1-50
For \$X = 0 to 49
\$array[\$X] = \$X + 1
Next

;_ArrayDisplay(\$array)

;Shuffle array
For \$X = 0 to 49
\$temp = \$array[\$X]
\$random = Random(0,49,1)
\$array[\$X] = \$array[\$random]
\$array[\$random] = \$temp
Next

;_ArrayDisplay(\$array)

\$string = _ArrayToString(\$array,",")

MsgBox(0,"",\$string)```

EDIT - Smoke am I that predictable? Maybe I should make you a sweet signature like mine. You could be AutoIt Assault

Edited by weaponx

##### Share on other sites

Awesome! Thanks for the help!!!

##### Share on other sites

EDIT - Smoke am I that predictable?

Lately you have been

Awesome! Thanks for the help!!!

Help? We call that giving a man a fish here (rather than teaching him too )

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.

## Create an account

Register a new account

• ### Recently Browsing   0 members

×

• Wiki

• Back

• #### Beta

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