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
        47.8985
        rndnum% = INT(RND * 100)
        PRINT rndnum%
        47
        rndnum% = INT(RND * 50) + 50
        rndnum% = INT(RND * 40) - 20
    Back     |     Home     |     Next     |