Jump to content

Random Numbers


bouval
 Share

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

Link to comment
Share on other sites

  • Moderators

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.

Link to comment
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
Link to comment
Share on other sites

  • Moderators

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.

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