Need some help with randomizing

If you need help with a project or need to know how to do something specific in VB.NET then please ask your questions in here.
Forum rules
Please LOCK your topics once you have found the solution to your question so we know you no longer require help with your query.
3 posts Page 1 of 1
Contributors
User avatar
jtlusco
VIP - Donator
VIP - Donator
Posts: 138
Joined: Mon Jul 12, 2010 11:28 pm

Need some help with randomizing
jtlusco
I have 5 Functions setup and I want to randomize what one is triggered when any key is pressed on a keyboard.
Any assistance would be appreciated.
Have you been scripted today
Image
User avatar
CodenStuff
Site Admin
Site Admin
Posts: 4389
Joined: Tue Aug 04, 2009 1:47 am

A simple random with a select may do the job.

On your keydown or keypress event (whatever you're using) do something like:
Code: Select all
        Dim r As New Random
        Select Case r.Next(0, 5)
            Case 0
                function1
            Case 1
                function2
            Case 2
                function3
            Case 3
                function4
            Case 4
                function5
        End Select
Welcome to CodenStuff.com Learn Code, Love Code. Thank you for being a member of the community.
User avatar
jtlusco
VIP - Donator
VIP - Donator
Posts: 138
Joined: Mon Jul 12, 2010 11:28 pm

Re: Need some help with randomizing
jtlusco
Thanks I forgot all about using a case statement. Now I just need to get the background music to keep playing when the other sounds play.
Other then that this works great. It is an application that I am making for my 11 month old so he can smack the keyboard on an old netbook and have fun with animals popping up and making sounds.

So if anyone knows how to get the sound to work correctly I would love an answer as I have been unable to find an answer.
Have you been scripted today
Image
3 posts Page 1 of 1
Return to “Coding Help & Support”