In this programme we'll learn about generating random numbers, which is useful for games.
00:08
This is a simple programme that will simulate rolling a classic six sided dice.
00:13
We start by creating our boilerplate code.
00:16
In this programme we also have a new section called Globals, which I'll explain in a moment, followed by our subprograms and main programme.
00:25
Let's write the first command in the globals section and talk about it.
00:29
Static Random.
00:30
Randomgenerator, new random, open and close brackets, semicolon.
00:39
Notice that random is a kind of new data type we haven't seen before.
00:44
It's actually a class and randomgenerator, is a variable.
00:49
We've already learned that variables can only be used within the subprogram in which they're declared.
00:54
They are known as local variables because they are local to and belong to the subprogram they are declared within.
01:02
Generally speaking, you should be declaring variables within a subprogram because they use memory to store values only when they're needed and then when the subprogram exits or returns, the memory is given back to the operating system to be used for something else.
01:17
Using memory like this is considered good practise.
01:21
However, in this programme we're going to learn that it's possible to declare variables outside of a subprogram and that makes them global variables, hence declaring them in the global section.
01:33
Global just means all the subprograms within the same class can use them at any time.
01:37
But the downside is they hold the memory for the entire time the programme is running.
01:42
This is acceptable for very small programmes, but programmes requiring a lot of data would be hogging the memory unnecessarily.
01:49
So in most cases they're not used with random numbers.
01:54
There's a reason to use a global variable.
01:57
Random is a special class built into the system commands that calculates a sequence of numbers that appear random.
02:04
This sequence is stored in our random generator variable.
02:07
We don't want to waste time processing a new sequence of random numbers every time we ask for one.
02:13
Instead, we'll use one set of numbers.
02:16
The first time we ask for a number we want the first one in the sequence.
02:20
And the second time we ask for a number, we want the second one in the sequence.
02:24
So this is a good case for using a global variable.
02:27
We've sacrificed memory for execution speed.
02:31
The compromise and decision between speed and memory is one that occurs regularly in programming.
02:38
This is a bit overkill in this programme because we only want one number, but we're laying the foundations for more interesting programmes later by learning These things in small steps.
02:49
Let's write a subprogram to roll a six sided dice.
02:53
Static int rolledice no data to receive, so open and close brackets.
02:59
We used int because we want a whole number one to six.
03:04
Int result of the roll equals our random number generator's next number, which is in the range of 1 and 7.
03:13
The upper bounds are exclusive, hence why this is 7 and not 6.
03:17
Slightly confusing, but just remember to add 1 to whatever the maximum number is that you want.
03:24
You may have noticed that we use the dot syntax here, just like the writeline Command.
03:29
And because Next requires data, the numbers 1 and 7, they're separated with a comma and enclosed in brackets.
03:38
Now, in the main programme, we can make use of our dice roller.
03:41
Int number equals the return value from our rolldice subprogram.
03:47
Put that on the screen with console, WriteLine and string formatting $urolleda followed by our number variable.
03:57
We can now run the programme and we got two as our random number.
04:02
If we run the programme again, we may get a different number.
04:05
Because it calculates a new sequence of numbers, there's actually no such thing as a random number in a computer.
04:11
They're deterministic.
04:13
That means they are simply calculations that produce what look like a random sequence to a human being.
04:19
For example, take 23 divided by 7.
04:22
It gives you a seemingly random sequence of numbers after a decimal place.
04:27
2, 8, 5, 7, 1 and 4.
04:30
Although the pattern does then repeat, so it can be exploited.
04:35
The number used in the calculation is known as a seed.
04:38
That's how early computer games were often exploited by players, by working out the sequence of random numbers being used.
04:46
It's these kinds of calculations that produce random numbers in a computer, albeit the calculations are more sophisticated.
04:54
In the case of C, it uses the number of clock cycles since the computer was switched on as the seed for the calculation.
05:01
That means if you turn two identical computers on at the same time, they would generate the same random numbers.