In these tough economic times, more people are playing lotto to hit it big. We have written a MATLAB program that chooses unique and random numbers for a lotto. The inputs are the number of numbers to pick, and the range in which the numbers can be picked.
The MATLAB program can be downloaded as a Mfile (better to download it, as single quotes from the web-post do not translate correctly with the MATLAB editor). The html file showing the mfile and the command window output is also available.
Can you modify this program to put the numbers in an ascending order?
How does the random number generator work?
How can I get random numbers that are different every time I run the program?
%% PICKING THE LOTTO NUMBERS
% Language : Matlab 2007a
% Authors : Autar Kaw
% Last Revised : November 10, 2008
% Abstract: This program chooses randomly m unique numbers to play
% the lotto. Lotto numbers allowed are positive integers from
% xlow to xhigh
disp(‘This program chooses randomly m unique numbers to play’)
disp(‘the lotto. Lotto numbers allowed are positive integers from’)
disp(‘xlow to xhigh’)
% xlow= lowest integer allowed
% xhigh = highest integer allowed
% number of integers to be picked
fprintf(‘Lowest integer allowed=%g’,xlow)
fprintf(‘\nHighest integer allowed=%g’,xhigh)
fprintf(‘\nNumbers to be picked=%g’,m)
% Using the random number generator rand to get the lotto numbers.
% rand generates numbers between 0 and 1. So we multiply that by xhigh-xlow+1
% and shift it by xlow and then floor it to get the integer part.
% flag= variable that keeps track of previous numbers
% being same or different from the number picked
disp(‘The lotto numbers picked are’)
disp (‘ ‘)
This post is brought to you by Holistic Numerical Methods: Numerical Methods for the STEM undergraduate at http://numericalmethods.eng.usf.edu.