RANDOMIZE and RND

        I had originally planned to talk about User defined data types now, but after a little bit of consideration I decided that it would be best to save that for the Advanced discussion and technique portion of this tutorial. So you'll have to settle for learning about how to obtain random numbers. You may think that this is bunk and you are moving on to the next part, just wait. There are some explanations in here that you'll probably want to see.
        First of all, why the heck would you want to generate a random number? Well the anwser is simple, if you are programming a game often you want the result of some action to be random, and this is how you would do that. first of all you need to do what is called re-seed the random number generator. You would do this with RANDOMIZE, however if you just use RANDOMIZE when you run the program you will be asked for a seed number. To avoid this you must supply RANDOMIZE with a seed-number. Any numeric value will work. The problem is, is that you will always get the same 'random' numbers generated if you always use the same seed number. So My method (as well as many others) to avoid this is to RANDOMIZE TIMER. TIMER is a function that returns the time in seconds since midnight.

        Definition: Function: a function is something in a programming language that 'returns' a value, that is, you can set a variable equal to a function and the variable will become the value that the functionn 'returns'. Often to return a value a function must take 'parameters' to calculate the return value. Parameters can be variables or just values, the type of value and number of parameters that a function takes is dependent on the function. Also, when a function is used we say that the function has been 'called'.

So, you will allways get a good and random number generated if you seed in this manner. Here is the code to do that:
        RANDOMIZE TIMER
        Okay, now to actually obtain the random number. To obtain the random number you would use the RND function. The RND function returns a random number, based on the seed number, between 0 and 1. Often you will want a number that is between some range of numbers, say 0 and 100 just for example, so to do this you would multiply RND by 10 like this:
        PRINT RND * 100

Output:
        47.8985

        One thing that I should note before I go on any further is that you only need to do RANDOMIZE TIMER once, perhaps at the begining of the program. Okay now the above example is good, but you wont always want the decimal digits, so to get rid of them we 'force' the random number generated to be an integer value, that is, one without digits behind the decmial point. We would do this with:
        rndnum% = INT(RND * 100)
        PRINT rndnum%

Output:
        47

        Okay, now we can generate random numbers between 0 and some other number like 100. What if you wanted to generate a number from 50 to 100, well it's elementary my dear student. Just multiply RND by 50, and add 50! Like this:
        rndnum% = INT(RND * 50) + 50

        What if we wanted to generate numbers between -20 and 20? Well this time subtract. First multiply by 40, then subtract 20, Like this:
        rndnum% = INT(RND * 40) - 20

        If you don't believe me, just try it yourself! Okay that's all for this aside, let's get back to the real grit and grime of the language.

    Back         Home         Next