Contents
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
clc clear all disp('This program chooses randomly m unique numbers to play') disp('the lotto. Lotto numbers allowed are positive integers from') disp('xlow to xhigh') disp(' ')
This program chooses randomly m unique numbers to play the lotto. Lotto numbers allowed are positive integers from xlow to xhigh
INPUTS
xlow= lowest integer allowed
xlow=1; % xhigh = highest integer allowed xhigh=53; % number of integers to be picked m=6; disp ('INPUTS') fprintf('Lowest integer allowed=%g',xlow) fprintf('\nHighest integer allowed=%g',xhigh) fprintf('\nNumbers to be picked=%g',m) disp(' ') % 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.
INPUTS Lowest integer allowed=1 Highest integer allowed=53 Numbers to be picked=6
SOLUTION
i=1; while (i<=m) lottonum(i)=floor(xlow+rand*(xhigh-xlow+1)); % flag= variable that keeps track of previous numbers % being same or different from the number picked flag=0; for j=1:i-1 if (lottonum(i)==lottonum(j)) flag=1; end end if flag==0 i=i+1; end end
OUTPUT
disp(' ') disp('OUTPUT') disp('The lotto numbers picked are') fprintf('%g ',lottonum) disp (' ')
OUTPUT The lotto numbers picked are 28 47 10 52 15 14