Random numbers can be used in simple games when the programmer wants a number to be unknown - the programmer themselves won't know what it's going to be
Most programming Languages have functions to generate random numbers. What is the function that OCR Exam Reference Language uses?
random(x, y)
In the function random(x, y) , if the values of x and y are integers , then a randominteger will be generated - if x and y are real numbers , then a randomreal number is generated
What does the function random(1, 6) do?
It generates a random integer between 1 and 6
What does the function random(1.0, 6.0) do?
It generates a random real number between 1.0 and 6.0
The code below is an example of how random numbers can be used to simulate a roll of a 6-sided dice. Explain how it works?
The first line generates either 1, 2, 3, 4, 5 or 6 and assigns it the variable 'roll' - The second line prints the number that is randomly generated
FOR loops can be used when you want to generate a whole bunch of randomnumbers
The code below uses a FOR loop that will generate 3 random integers from 1 to 10
You can used random numbers to make random selections
Instead of outputting the randomnumber that you generated, you can use it to randomlygenerate another event
The code below simulates a coin toss - there are two outcomes , heads or tails - this can be simplified in programming terms 0 and 1 , where 0 = heads , and 1 = tails
Random numbers are handy to use with arrays - you can generate a random number then pick the element in that position of the array
The code below uses a randomnumber to select a piece of fruit from the array