Jump to content

2 loop running simultaniously?


MariusN
 Share

Recommended Posts

Can a script contain 2 loops that can run simultaniously? The 1 must generate random numbers, while the other

stops the loop, gets a number, and continues...If YES (i hope), could you maby give me an example? I have compiled a lotto program that works 100%, but to get 6 numbers, you have to actually CLICK the button SIX times to get random numbers...otherwise all numbers are the same...lol

Link to comment
Share on other sites

  • Moderators

MariusN,

Look at this - it avoids duplicates as well :) :

#include <Array.au3>

Global $aNumbers[6]
For $i = 0 To 5
    Do
    $iNumber = Random(1, 49, 1)
    Until _ArraySearch($aNumbers, $iNumber) = -1
    $aNumbers[$i] = $iNumber
Next

_ArrayDisplay($aNumbers)

M23

Public_Domain.png.2d871819fcb9957cf44f4514551a2935.png Any of my own code posted anywhere on the forum is available for use by others without any restriction of any kind

Open spoiler to see 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

 

Link to comment
Share on other sites

AutoIt is not multi-threaded, so it can not run 2 loops at the same time.

However, i'm pretty sure it will be possible to tweak your programs' logic

to get arounnd that, there's actually surprisingly few cases (IMHO)

where multi threading is absolutely necessary - and i doubt if generating

6 random numbers is one of those

whim

Link to comment
Share on other sites

MariusN,

Look at this - it avoids duplicates as well ;) :

#include <Array.au3>

Global $aNumbers[6]
For $i = 0 To 5
    Do
    $iNumber = Random(1, 49, 1)
    Until _ArraySearch($aNumbers, $iNumber) = -1
    $aNumbers[$i] = $iNumber
Next

_ArrayDisplay($aNumbers)

M23

Thank you M23...this is exactly what i wanted. Is there a way of "pausing" so to speak this function?What i mean is lets say i want the program to display only ONE no out of the 6 ( the amount of no's ill have at the end).for at least 250 ms intervals? The "LOOP" must basicly carry on looping, and A no (only 1 at a time till all 6 are there) have to be retrieved from it every 250ms. Is this possible?

If this is a stupid question, please ignore...:)

Link to comment
Share on other sites

  • Moderators

MariusN,

Because I am feeling generous today: :)

#include <GUIConstantsEx.au3>
#include <Array.au3>

Global $iMax_Numbers = 6, $iLowest_Num = 0, $iHighest_Num = 50 ; Adjust these to change the number and the range of random numbers produced

$hGUI = GUICreate("Random Numbers", 180, 260)

$hLabel = GUICtrlCreateLabel("How many numbers (1-" & $iMax_Numbers & ")?", 10, 10, 200, 20)
$hInput = GUICtrlCreateInput("", 10, 30, 50, 20)
$hUD = GUICtrlCreateUpdown($hInput)
GUICtrlSetLimit(1, 6)
$hButton = GUICtrlCreateButton("Go!", 10, 70, 80, 30)

$hNumber = GUICtrlCreateLabel("", 10, 120, 200, 200)
GUICtrlSetFont(-1, 96, 800)

GUISetState()

While 1

    Switch GUIGetMsg()
        Case $GUI_EVENT_CLOSE
            Exit
        Case $hButton
            Roll()
    EndSwitch

    If GUICtrlRead($hInput) < 1 then GUICtrlSetData($hInput, 1)
    If GUICtrlRead($hInput) > $iMax_Numbers then GUICtrlSetData($hInput, $iMax_Numbers)

WEnd

Func Roll()

    Local $aNumbers[$iMax_Numbers]
    Local $iCount = GUICtrlRead($hInput)
    For $i = 0 To $iCount - 1
        Do
        $iNumber = Random($iLowest_Num, $iHighest_Num, 1)
        Until _ArraySearch($aNumbers, $iNumber) = -1
        $aNumbers[$i] = $iNumber
        GUICtrlSetData($hNumber, $iNumber)
        Sleep(1000) ; Adjust this to keep the number on screen longer
    Next

    GUICtrlSetData($hNumber, "")

EndFunc

M23

Public_Domain.png.2d871819fcb9957cf44f4514551a2935.png Any of my own code posted anywhere on the forum is available for use by others without any restriction of any kind

Open spoiler to see 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

 

Link to comment
Share on other sites

MariusN,

Because I am feeling generous today: ;)

#include <GUIConstantsEx.au3>
#include <Array.au3>

Global $iMax_Numbers = 6, $iLowest_Num = 0, $iHighest_Num = 50 ; Adjust these to change the number and the range of random numbers produced

$hGUI = GUICreate("Random Numbers", 180, 260)

$hLabel = GUICtrlCreateLabel("How many numbers (1-" & $iMax_Numbers & ")?", 10, 10, 200, 20)
$hInput = GUICtrlCreateInput("", 10, 30, 50, 20)
$hUD = GUICtrlCreateUpdown($hInput)
GUICtrlSetLimit(1, 6)
$hButton = GUICtrlCreateButton("Go!", 10, 70, 80, 30)

$hNumber = GUICtrlCreateLabel("", 10, 120, 200, 200)
GUICtrlSetFont(-1, 96, 800)

GUISetState()

While 1

    Switch GUIGetMsg()
        Case $GUI_EVENT_CLOSE
            Exit
        Case $hButton
            Roll()
    EndSwitch

    If GUICtrlRead($hInput) < 1 then GUICtrlSetData($hInput, 1)
    If GUICtrlRead($hInput) > $iMax_Numbers then GUICtrlSetData($hInput, $iMax_Numbers)

WEnd

Func Roll()

    Local $aNumbers[$iMax_Numbers]
    Local $iCount = GUICtrlRead($hInput)
    For $i = 0 To $iCount - 1
        Do
        $iNumber = Random($iLowest_Num, $iHighest_Num, 1)
        Until _ArraySearch($aNumbers, $iNumber) = -1
        $aNumbers[$i] = $iNumber
        GUICtrlSetData($hNumber, $iNumber)
        Sleep(1000) ; Adjust this to keep the number on screen longer
    Next

    GUICtrlSetData($hNumber, "")

EndFunc

M23

Thanks M23 :)
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...