Jump to content

Randomization of functions beeing fired


Recommended Posts

Hi, i where just wondering is there a to get a script starting a new function but with a random order?

ie. say i have 4 functions, and i want them to fire in an random order

My first thought was about using an array, and setting a random fire event on those 4 arrays,

but i can't remember the random code

what iv written so far regarding it, the arrays works, but for now it selects all, i want it to random select one :)

Local $arr[4]  
 $arr[0]=Func01()
 $arr[1]=func02()
 $arr[2]=func03()
 $arr[3]=func04()
 Local $i
 For $arr[04] = 0 to 4 - 1 
     MsgBox(0,"test",$arr)
 Next
 func Func01()
    MsgBox(0,"test","Fired event 01")
EndFunc
func Func02()
    MsgBox(0,"test","Fired event 02")
EndFunc
func Func03()
    MsgBox(0,"test","Fired event 03")
EndFunc
func Func04()
    MsgBox(0,"test","Fired event 04")
    EndFunc
Edited by pezo89
Link to comment
Share on other sites

Hi

You can do random by using the function Random(MIN, MAX, FLAG).

FLAG 1 makes the function return an integer instead of floating point numbers.

Though I wouldn't do what you're doing if I were you. Keep in mind that strictly speaking,

$arr[0]=Func01()
$arr[1]=Func02()
$arr[2]=Func03()
$arr[3]=Func04()

this part is telling the computer to store the return value of FuncXX in the cell $arr[XX], which technically, is not what you are trying to do. I recommend using Switch Case or If ElseIf Else.

Felastine.

Edited by Felastine
Link to comment
Share on other sites

This is one two ways.

Global $Msg
Local $arr[4] = ["Func01()", "func02()", "func03()", "func04()"]

While 1

    Call("Func" & StringRight("0" & Random(1, 4, 1), 2))
    ;Or
    ;Execute($arr[Random(0, 3, 1)])

    If $Msg = 1 Then Exit
    Sleep(10)
WEnd

Func Func01()
    $Msg = MsgBox(0, "test", "Fired event 01", 1)
EndFunc   ;==>Func01
Func Func02()
    $Msg = MsgBox(0, "test", "Fired event 02", 1)
EndFunc   ;==>Func02
Func Func03()
    $Msg = MsgBox(0, "test", "Fired event 03", 1)
EndFunc   ;==>Func03
Func Func04()
    $Msg = MsgBox(0, "test", "Fired event 04", 1)
EndFunc   ;==>Func04

Edit: Added Call method.

Edited by Malkey
Link to comment
Share on other sites

Or without Execute()

Global $Msg
Local $arr[4] = ["Func01()", "func02()", "func03()", "func04()"]

While 1
    Switch Random(0, 3, 1)
        Case 0
            Func01()
        Case 1
            Func02()
        Case 2
            Func03()
        Case 3
            Func04()
    EndSwitch
    If $Msg = 1 Then Exit
    Sleep(10)
WEnd

Func Func01()
    $Msg = MsgBox(0, "test", "Fired event 01", 1)
EndFunc   ;==>Func01
Func Func02()
    $Msg = MsgBox(0, "test", "Fired event 02", 1)
EndFunc   ;==>Func02
Func Func03()
    $Msg = MsgBox(0, "test", "Fired event 03", 1)
EndFunc   ;==>Func03
Func Func04()
    $Msg = MsgBox(0, "test", "Fired event 04", 1)
EndFunc   ;==>Func04

I think this is what Felastine meant also.

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