# Using Random funtion

## Recommended Posts

Hi,

I have been using autoit for a couple years as a software distribution tech and mostly using runwait and such to run msi's and such, but I have been wanting to expand my knowledge.

I live in winnipeg canada and we have a lotto here called 6/49 you need 6 numbers of 49. I wrote a script that randomizes numbers 1-49 and if it is already in the array it will go back to randomizing...

********code*********

#include <Array.au3>

\$LowerLimit = 1

\$UpperLimit = 49

Dim \$ArrayVar

Dim \$avArray[1]

call("lotto")

Func Lotto()

\$i = 0

Do

\$RandomNumber = Random(\$LowerLimit,\$UpperLimit,1)

\$Pos = _ArraySearch (\$avArray, \$RandomNumber, 0, 0, 0, True)

If \$Pos = -1 Then

EndIf

\$i = \$i + 1

Until UBound(\$avArray, 1) = 44

EndFunc

_ArrayDisplay( \$avArray, "Whole array" )

**********code*************

The script runs really good except if i want it to randomize the whole 49 numbers. if I ask for it to calculate 43 of 49 numbers i get an array in about 3 secs, if i ask for 44 I have never seen it finish. I have a dual xeon with 6GB of ram so it is not processing power.

if anyone can explain this to me i would really apreciate it

##### Share on other sites

Why did you put in the partial search = true in the ArraySearch?

Try this version which works fine for me:

`\$Pos = _ArraySearch(\$avArray, \$RandomNumber)`
Edited by Jos

Live for the present,
Dream of the future,
Learn from the past.

##### Share on other sites

ben7winnipeg,

This runs for me at between 1 and 20 ms and checks for duplicates. It uses a second array to check for duplicates:

```#include <Array.au3>

\$begin= TimerInit()

\$LowerLimit = 1
\$UpperLimit = 49
\$Required = 49  ; Set this to the value you require

Global \$CheckVar[50], \$ArrayVar[\$Required + 1]

Call("lotto")

ConsoleWrite(TimerDiff(\$begin) & @CRLF)

For \$i = 1 To \$Required - 1
For \$j = \$i + 1 To \$Required
If \$ArrayVar[\$i] = \$ArrayVar[\$j] Then ConsoleWrite("Duplicate" & @CRLF)
Next
Next

_ArrayDisplay(\$ArrayVar, "Whole array")

Func Lotto()
\$i = 1

While 1

\$RandomNumber = Random(\$LowerLimit, \$UpperLimit, 1)

If \$CheckVar[\$RandomNumber] = "" Then
\$ArrayVar[\$i] = \$RandomNumber
\$CheckVar[\$RandomNumber] = 1
\$i += 1
If \$i > \$Required Then ExitLoop
EndIf

WEnd

EndFunc  ;==>Lotto```

The code writes the generation time in SciTE. Obviously this depends on how many times Random produces an already used random number.

Hope this is of use.

M23

Any of my own code posted anywhere on the forum is available for use by others without any restriction of any kind._______My UDFs:

Spoiler

ArrayMultiColSort ---- Sort arrays on multiple columns
ChooseFileFolder ---- Single and multiple selections from specified path treeview listing
Date_Time_Convert -- Easily convert date/time formats, including the language used
ExtMsgBox --------- A highly customisable replacement for MsgBox
GUIExtender -------- Extend and retract multiple sections within a GUI
GUIFrame ---------- Subdivide GUIs into many adjustable frames
GUIListViewEx ------- Insert, delete, move, drag, sort, edit and colour ListView items
GUITreeViewEx ------ Check/clear parent and child checkboxes in a TreeView
Marquee ----------- Scrolling tickertape GUIs
NoFocusLines ------- Remove the dotted focus lines from buttons, sliders, radios and checkboxes
Notify ------------- Small notifications on the edge of the display
Scrollbars ----------Automatically sized scrollbars with a single command
StringSize ---------- Automatically size controls to fit text
Toast -------------- Small GUIs which pop out of the notification area

##### Share on other sites

Wow Melba, You are FRICKEN AWSOME! it ran so quick i couldnt tell you in the milli seconds. i really apreciate the response. I have a lot of interest in autoit, I used to dabble in Vb but just ran into too many roadblocks with my company. i love the compiled exe's! hiding serial numbers and package locations from the numb nut user has made all the difference especially using the piece o shite LAndesk...

## Create an account

Register a new account

×

• Wiki

• Back

• Git