Click here to Skip to main content
15,881,856 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
How can i create a script to do random functions based on the random number function?
like for instance, i have 4 different functions, so if the random number function chooses 3 it'll execute my 3rd action, if it chooses 1 it'll execute 1. Any Ideas?
Posted
Comments
ZurdoDev 7-Mar-12 11:13am    
Where are you stuck?

A small sample of how to this with VBScript, enjoy!
VBScript
Function DoStuff()
    Dim what, result
    what = Rand(1,4)
    Select Case what
        Case 1
            result = Action1()
        Case 2
            result = Action2()
        Case 3
            result = Action3()
        Case 4
            result = Action4()
    End Select
End Function

'Choose an integer between min and max
Function Rand(min, max)
    max=4
    min=1
    Randomize
    Rand = (Int((max-min+1)*Rnd+min))
End Function

' Define your own actions here
Function Action1()
    Action1 = "Result 1!"
End Function

Function Action2()
    Action2 = "Result 2!"
End Function

Function Action3()
    Action3 = "Result 3!"
End Function

Function Action4()
    Action4 = "Result 4!"
End Function


Regards,

Manfred
 
Share this answer
 
If the number of choices is small enough, you could just use SELECT CASE.
Select Case (Int(Rnd * 3) + 1)
    Case 1
        SomeFunction1
    Case 2
        SomeOtherFunction
    Case 3
        SomeOtherMethod
End Select
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900