Contents
PICKING THE LOTTO NUMBERS WHILE SKIPPING SOME
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, while numbers asked to be skipped are not chosen
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, while numbers asked to be skipped are not chosen') disp(' ')
This program chooses randomly m unique numbers to play the lotto. Lotto numbers allowed are positive integers from xlow to xhigh, while numbers asked to be skipped are not chosen
INPUTS
xlow= lowest integer allowed
xlow=1; % xhigh = highest integer allowed xhigh=53; % number of integers to be picked m=6; % numbers to be skipped lottoskip=[ 2 5 7 12 21]; disp ('INPUTS') fprintf('Lowest integer allowed=%g',xlow) fprintf('\nHighest integer allowed=%g',xhigh) fprintf('\nNumbers to be picked=%g\n\n',m) disp('Lotto numbers skipped') lottoskip 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 Lotto numbers skipped lottoskip = 2 5 7 12 21
SOLUTION
number of lotto numbers to be skipped from being chosen
mskip=length(lottoskip); 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 % also it checks if the numbers asked to skipped are % not chosen flag=0; % checking against previous numbers chosen for j=1:1:i-1 if (lottonum(i)==lottonum(j)) flag=1; end end % checking against numbers to be skipped for j=1:1:mskip if (lottonum(i)==lottoskip(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 50 25 23 45 28 11