AQA Computer Science GCSE
Programming Concepts - Random Numbers
Random numbers get used all the time in programming.
This is a fairly specific area of the syllabus. It might come up in an exam, but it's not too tricky to deal with by using some logic if it does.
Creating Random Numbers
The idea is pretty simple here: use code to create a random number between two limits. This will most likely need to be assigned to a variable. In general you'd only ever be asked to deal with creating an integer.
dieThrow <- RANDOM_INT(1, 6)
The code above will create the result of throwing a single 6 sided die - it will return a random choice of the numbers 1, 2, 3, 4, 5 or 6.
If I wanted to create different ranges of numbers I can simply change the parameters passed to RANDOM_INT(). So, if I need a random number between 1 and 8 (say I'm rolling 1d8) I go RANDOM_INT(1, 8).
The way to do this in Python is:
# creating random integer
dieThrow = random.randint(1, 6)
Note the need to import the external function library random. Random numbers aren't included as standard in Python so this really useful library needs to be imported at the top of your code.
More Dice and Bigger Numbers!
Make sure that you think about random numbers and probability a little before you get confused.
If, for example, you want to model the result of the throw of 2 six sided dice (2d6) you need to create each throw randomly and then add the numbers together. This is totally different from creating a random number between 2 and 12 - the probability of rolling a 7 on 2d6 is, for example 7/36 (there are 7 ways to make 7 on 2d6) whereas there is only a 1/36 chance of rolling a 12 on the same dice.
diceThrow <- RANDOM_INT(1, 6) + RANDOM_INT(1, 6)
It is, of course, entirely possible to pull a random number from a larger set as well. Say we want a random number between 1 and 1000, we'd simply use:
randNumber <- RANDOM_INT(1, 1000)
I can also use random numbers to simulate a coin flip. All I need to do is say that 0 is a heads and 1 is a tails and then chose RANDOM_INT(0, 1). The harder bit here is using selection to deal with the result.
Using Random Numbers with Arrays
Random numbers can be used to randomly choose an element from an array. Sometimes this can be exactly what you need.
Say I have an array of names and want to choose a random name from the array. I'd do it this sort of way:
nameArray <- ["Alan", "Brenda", "Cedric", "Doris", "Edna", "Fergal", "Grace", "H"]
randomNumber <- RANDOM_INT(0, LEN(nameArray)-1)
theName <- nameArray[randomNumber]
The use of LEN(nameArray)-1 is necessary because we always assume arrays are indexed from 0 - unless an exam question says otherwise. The length of the array is 8 but nameArray will cause an error as there isn't anything at nameArray (an index out of range error specifically). We also need to start from 0 and not 1, otherwise Alan (the element at index 0 of the array) can never be chosen.
Hopefully random numbers shouldn't cause too many problems. Just use your common sense and you should be OK.