Thursday, May 12, 2011

Effectively Utilizing the Random Number Generator!

So today (this morning), I felt like it would be a good idea to give a quick once-over of the Random Variables.

If you've never done any programming, you've probably never run into any of these. Well, it's time to learn! If you've ever wanted to easily pick between a set of objects or wait out a random amount of time between fire-offs, then the random number generator is for you!

Example 1: Time usage.
Let's say you've got this awesome background noise that you want to go off every now and then, for no particular reason. Going the next step, you don't want it to happen with a pattern so that your players can figure out it happens every 30 seconds. In order to do this, all we need to do is setup the usual delay. Right click on an empty space and create a new Float Variable. Under the float menu, you'll see Random Float. In the first section of the variable properties, you'll see a minimum and a maximum. This is also known as the range. If you want it to happen every 30s +/- 15, then set you minimum to 15, and your maximum to 45. Easy as that. Now, loop the completion of the event back into the start of the delay and you've got it repeating at random 30 +/- 15.

Example 2: Random Functions.
So maybe you've played a lot of pokemon and you're into the whole "random encounter" situation. Well let's say you built a button. And every time you pressed the button, you had a 1 in 6 chance of being killed. (russian roulette). In order to achieve this, you would set up your kill the player function as usual and hook it up to a comparison INT. The A value would be the Random Int with a range of 1,6. The other value would be whatever you wanted the death number to be. When A == B, death ensues!

That pretty much wraps up the basics of the Random Number Generator. If you have any questions, feel free to comment!

No comments:

Post a Comment