3.4: Random Numbers

Right, enough playing it safe, lets play the lottery and win a million!

In the last section we saw that the chances of winning the lottery are very slim. Thanks to the perfectly inescapable digital logic of BASIC, we were able to see and exact, inarguable number showing how unlikely we are to win. Yay BASIC.

Except I simply cannot get this idea out of my head that I could hire someone else to make my bed every day. So screw it, let's enter the lottery!

As I said in the last section, I'm no good at picking numbers. If only there was some way we could get our computer to pick our numbers for us.

Well, as lucky would have it, BASIC can help us with this. We just need to learn a new command. Read on to find out.

Random Numbers

It has always been necessary for computers to have the ability to produce random numbers.

They are probably most commonly used in video games, as these tend to be more fun when they're unpredictable. Think about Pacman for instance. Imagine if every ghost just ran towards Pacman constantly: the game would be way too difficult to play. So they added some randomisation to it. Not all of the ghosts are actually chasing him, some are moving around in a random pattern to stop the game being too hard.

Or think about using a music playing app to play songs on shuffle. The feature of "shuffle" was introduced because sometimes its more fun to not know what song is coming up next, like when you're at a party. For the computer to decide what track to play next, it needs to be able to generate a random number.

Making random numbers in BASIC couldn't be simpler. We just need to use the right command word, which is RND. Try this (and hit enter):

>PRINT RND

When I did that, I got this:

>PRINT RND
        2591288813393862

You will have got a different number, becaused the RND command (drum roll) produces random numbers!

OK GREAT GOOD WORK EVERYBODY THATS RANDOM NUMBERS IN BASIC WE CAN GO HOME NOW...

Ok so although this works, it's not very useful. BASIC is literally producing a totally random number each time. It could be anything. I tried it six times:

>PRINT RND
        2591288813393862
>PRINT RND
        3186392616412338
>PRINT RND
        5300298957303678
>PRINT RND
        2807281372577910
>PRINT RND
        7759634565030170
>PRINT RND
        3020759808156636

In real life this would not be very useful. When we want to produce a random number that we are actually going to use, we usually want it to be within a range.

Think of a pair of dice in a board game, for instance. Although the number you get from dice is random, it still has to be between 1 and 6. Most games would not work if the dice could throw 3020759808156636.

So BASIC allows us to cap the random number with a maximum. To do this, you put the number in brackets directly after RND:

>PRINT RND(6)

When I do that, I get this:

>PRINT RND(6)
        4

You will most likely get another number, but it will still be between 1 and 6 because we put the 6 in brackets after the command.

Learn, But Do Not Over-learn

We had to put the 6 in brackets here because the RND command is a special type of command that can take a parameter, as in, you can give it "settings" that influence what it gives back to you. There are other commands that we'll look at later that also take parameters in brackets, but for now just enjoy the fact that we can simulate a dice roll without it giving back 3020759808156636.

Now that we know how to create random numbers with BASIC, we can use it to work out our lottery numbers and finally win those millions... (of made beds...)

Lottery Picker

First of all, start a NEW programme in BASIC:

>NEW
>

Now try writing out all of this in BASIC:

>1 PRINT "HELLO! LET'S MAKE YOU A LOTTERY WINNER!"
>2 PRINT RND(49)
>3 PRINT RND(49)
>4 PRINT RND(49)
>5 PRINT RND(49)
>6 PRINT RND(49)
>7 PRINT RND(49)
>

Then RUN the programme by typing RUN and pressing enter:

>1 PRINT "HELLO! LET'S MAKE YOU A LOTTERY WINNER!"
>2 PRINT RND(49)
>3 PRINT RND(49)
>4 PRINT RND(49)
>5 PRINT RND(49)
>6 PRINT RND(49)
>7 PRINT RND(49)
>RUN

This is what I got:

>RUN
HELLO! LET'S MAKE YOU A LOTTERY WINNER!
        34
        13
        48
        40
        3
        25

And there you have it! BASIC just gave you your winning lottery numbers! When you win, please send 10% to us. I promise if I enter the numbers I got there and I win, I'll send you 10%, so it's only fair.

Learn, But Do Not Over-learn

You may have noticed here that there is a chance BASIC could give us the same number twice. This would of course not be useful for picking lottery numbers. We can use additional clever coding to prevent numbers repeating, but we're not there yet and the chance of this happening is very small. So for now, if you do get the same number twice, simply RUN the programme again.

Lingo Time!

Well would you look at that, it's Lingo Time again! I said I would volunteer you for the quiz this time didn't I? Ok, well here is the question fresh from Si. Take a moment to think about it and click on your guess. The question is:

What is the name of the most commonly used number type in computer programming?
A: Digit
B: Ordinal
C: Numeric
D: Integer