Introduction
Play LOTTO
Research Lotto game rules (https://www.national-lottery.co.uk/), then design and implement a Matlab programme to generate several sets of Lotto numbers according to the amount of money specified.
The relevant information about the Lotto game rules taken from the National Lottery website are enumerated in the following bullet points:
Tickets cost £2 per play
Simply select six numbers from 1-59 or pick Lucky Dip for a random selection
Choose how many lines to play
The Matlab programme will operate the same way as the Lucky Dip feature of the website. The Matlab programme will randomly generate sets of six numbers from 1-59 that are suitable entries for Lotto. The main difference is that the Matlab programme asks for the amount of money to be played rather than the number of lines to play. From this amount of money, the number of lines to play is automatically computed and the sets of six numbers are generated.
Design
The Matlab programme will be comprised of 3 script files: lotto.m, generate.m, and numberOfDraws.m. The main programme is lotto. This main programme first prompts the user of how much money is the user going to bet for the lotto game. The prompt is done by using the input built-in function of Matlab. The input function is able to display a message on the command prompt, and then take whatever the user types in on the Matlab console. The amount of betting money is stored in the variable amount.
Then, the amount variable is inputted to the function numberOfDraws. This function is responsible for computing how many sets of six numbers will be drawn based on the betting money of the user. The price per ticket is defined within this numberOfDraws function (stored in variable p); this is done so that the price per ticket, which may change in the future, can be easily changed outside of the main programme and would not affect the operation of the whole programme. The numberOfDraws function utilizes the floor function in order to round down the result to the nearest whole number of lines to play. After the computation of the number of lines to play, the result is stored in the variable lines and is displayed on the command prompt using disp function as a notification for the user.
Next, the generate function is called in order to generate the sets of six numbers requested by the user. Input parameter for this function is the lines variable. First, the storage for the sets of six numbers are initialized using the zeros function . The generate function operates on a for-loop to generate 6 numbers per line of play. The rng function is utilized and is set to ‘shuffle’ to ensure that the random generator seed is different for every line of play. The randperm function is then used to generate a random permutation of six numbers from 1-59. The resulting six numbers are stored in the variable lottoNumbers and this variable is outputted by the generate function.
Finally, the main programme lotto displays the resulting 6-number combinations stored in LOTTONumbers on the console. Each row corresponds to one set of 6-number line of play.
The Matlab programme scripts are modularized in such a way that the main programme will focus only on the interface or display to the user—all the other operations, particularly the random number generation and the number of lines of play computation, are included in separate functions. This programme design makes it easier to modify code in case of changes in game rules, ticket prices, etc. The programme runs only once.
Results
The Matlab programme is tested for three types of inputs (amount of betting money): odd, even, and large number. The odd input tests if the numberOfDraws function would output a correct whole number even if the input is odd. The even input tests if the numberOfDraws function would also work with even values. The large number input tests if the programme can handle large number of lines of play. The results are shown in the following:
Odd Input = 5
Play LOTTO
How much money are you betting? 5
The amount is equivalent to 2 line/s to play.
LOTTONumbers =
12 27 34 46 16 32
13 50 19 48 34 6
Even Input = 6
Play LOTTO
How much money are you betting? 6
The amount is equivalent to 3 line/s to play.
LOTTONumbers =
3 4 11 6 23 15
34 33 5 41 32 9
48 9 40 58 56 14
Large Number Input = 31
Play LOTTO
How much money are you betting? 31
The amount is equivalent to 15 line/s to play.
LOTTONumbers =
5 32 57 8 23 27
52 5 34 58 3 12
57 48 8 31 45 13
55 14 53 39 42 36
15 55 6 14 49 39
54 55 7 39 26 14
41 29 18 46 24 38
19 15 32 10 9 30
28 41 18 48 33 31
16 44 33 2 54 48
45 28 2 43 50 22
30 9 33 49 26 45
25 27 58 35 51 57
55 32 16 57 22 40
23 3 20 15 55 36
Discussion
The Matlab programme works perfectly fine. It was able to generate 6-number permutations that range from 1-59. There were no repeated values for every line of play generated. Moreover, every line of play are unique combinations. The generate function is suitable for the task of generating random lotto numbers. The numberOfDraws function was accurate in finding the right number of lines of play given the amount of money.
References
About Lotto 2016, viewed 3 May 2016, <https://www.national-lottery.co.uk/games/lotto/about-lotto>.
The MathWorks Inc. 2013, MATLAB 8.0 Documentation: disp Display array, Natick, Massachusetts, United States.
The MathWorks Inc. 2013, MATLAB 8.0 Documentation: floor Round towards minus infinity, Natick, Massachusetts, United States.
The MathWorks Inc. 2013, MATLAB 8.0 Documentation: input Prompt for user input, Natick, Massachusetts, United States.
The MathWorks Inc. 2013, MATLAB 8.0 Documentation: randperm Random permutation, Natick, Massachusetts, United States.
The MathWorks Inc. 2013, MATLAB 8.0 Documentation: rng Control the random number generator used by RAND, RANDI, and RANDN, Natick, Massachusetts, United States.
The MathWorks Inc. 2013, MATLAB 8.0 Documentation: zeros Zeros array, Natick, Massachusetts, United States.
Appendix
lotto.m
% Play LOTTO
clear
clc
disp('Play LOTTO');
% Ask for Betting Money
amount = input('How much money are you betting? ');
lines = numberOfDraws(amount);
% Message about number of lines of play
disp(['The amount is equivalent to ', num2str(lines), ' line/s to play.']);
% Display LOTTO numbers on Matlab console
LOTTONumbers = generate(lines)
generate.m
function lottoNumbers = generate(lines)
% This function generates n sets of Lotto numbers according to the amount
% of money 'amount' specified.
lottoNumbers = zeros(lines, 6);
for i = 1:lines
rng('shuffle');
lottoNumbers(i,:) = randperm(59, 6);
end
end
numberOfDraws.m
function draws = numberOfDraws(amount)
% This function computes for the number of draws given the amount of
% betting money
p = 2; % Prize per 6-number draw
draws = floor(amount/p); % Number of draws, round down
end