PEscobar

How to start a random function?

6 posts in this topic

How do I make it so a random function within the script runs. What I tried was giving all the functions the same name but with a different number at the end. In my script I added the name of each function, then the random function with a range of 1-3 since that is what each function has at the end, but this of course did not work. What is another way of doing this without using the random function?

 

HotKeySet("{F1}", "endProg")

While(1)
    camera(random(1, 3, 1)()
 WEnd

 Func camera1()
    send("{right down}")
    sleep(random(200, 1000, 1)
    send("{right up}")
 EndFunc

 Func camera2()
   send("{left down}")
   sleep(random(200, 1000, 1)
   send("{left up}")
EndFunc

Func camera3()
   send("{up down}")
   sleep(random(200, 1000, 1)
   send("{up up}")
EndFunc

Share this post


Link to post
Share on other sites



Here's one way...

HotKeySet("{F1}", "RunFunc")

While (1)
    Sleep(100)
WEnd

Func RunFunc()
    Local $aFuncs[3] = [camera1, camera2, camera3]
    Call($aFuncs[Random(0, 2, 1)])
EndFunc   ;==>RunFunc


Func camera1()
    MsgBox(0, 0, "camera1")
EndFunc   ;==>camera1

Func camera2()
    MsgBox(0, 0, "camera2")
EndFunc   ;==>camera2

Func camera3()
    MsgBox(0, 0, "camera3")
EndFunc   ;==>camera3

 


AutoIt Absolute Beginners    Require a serial    Pause Script    Video Tutorials by Morthawt   ipify 

Monkey's are, like, natures humans.

Share this post


Link to post
Share on other sites

Or without the need to use an array:

HotKeySet("{F1}", "RunFunc")
Global $iMaxFuncs = 3 ; highest function number
While (1)
    Sleep(100)
WEnd

Func RunFunc()
    Call("camera" & Random(1, $iMaxFuncs, 1))
EndFunc   ;==>RunFunc


Func camera1()
    MsgBox(0, 0, "camera1")
EndFunc   ;==>camera1

Func camera2()
    MsgBox(0, 0, "camera2")
EndFunc   ;==>camera2

Func camera3()
    MsgBox(0, 0, "camera3")
EndFunc   ;==>camera3

 


My UDFs and Tutorials:

Spoiler

UDFs:
Active Directory (NEW 2017-04-18 - Version 1.4.8.0) - Download - General Help & Support - Example Scripts - Wiki
OutlookEX (NEW 2017-02-27 - Version 1.3.1.0) - Download - General Help & Support - Example Scripts - Wiki
ExcelChart (2015-04-01 - Version 0.4.0.0) - Download - General Help & Support - Example Scripts
Excel - Example Scripts - Wiki
Word - Wiki
PowerPoint (2015-06-06 - Version 0.0.5.0) - Download - General Help & Support

Tutorials:
ADO - Wiki

 

Share this post


Link to post
Share on other sites

And without the need to use Call

HotKeySet("{F1}", "RunFunc")

While (1)
    Sleep(100)
WEnd

Func RunFunc()
    Local $aFuncs[3] = [camera1, camera2, camera3]
    $aFuncs[Random(0, 2, 1)]()
EndFunc   ;==>RunFunc


Func camera1()
    MsgBox(0, 0, "camera1")
EndFunc   ;==>camera1

Func camera2()
    MsgBox(0, 0, "camera2")
EndFunc   ;==>camera2

Func camera3()
    MsgBox(0, 0, "camera3")
EndFunc   ;==>camera3

 


AutoIt Absolute Beginners    Require a serial    Pause Script    Video Tutorials by Morthawt   ipify 

Monkey's are, like, natures humans.

Share this post


Link to post
Share on other sites

#5 ·  Posted (edited)

PEscobar,

out of curiosity: Why do you need to run random functions?

Edited by water

My UDFs and Tutorials:

Spoiler

UDFs:
Active Directory (NEW 2017-04-18 - Version 1.4.8.0) - Download - General Help & Support - Example Scripts - Wiki
OutlookEX (NEW 2017-02-27 - Version 1.3.1.0) - Download - General Help & Support - Example Scripts - Wiki
ExcelChart (2015-04-01 - Version 0.4.0.0) - Download - General Help & Support - Example Scripts
Excel - Example Scripts - Wiki
Word - Wiki
PowerPoint (2015-06-06 - Version 0.0.5.0) - Download - General Help & Support

Tutorials:
ADO - Wiki

 

Share this post


Link to post
Share on other sites

@PEscobar, did you actually read those forumrules you were [pointed to by @Melba23?

I haven't seen the removed code but the question seems to be pretty close to your first thread!

Jos


Visit the SciTE4AutoIt3 Download page for the latest versions        Beta files                                                          Forum Rules
 
Live for the present,
Dream of the future,
Learn from the past.
  :)

Share this post


Link to post
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