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

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(‘ ‘)

%% 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.

%% 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: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 (‘ ‘)

This post is brought to you by Holistic Numerical Methods: Numerical Methods for the STEM undergraduate at http://numericalmethods.eng.usf.edu.

An abridged (for low cost) book on *Numerical Methods with Applications* will be in print (includes problem sets, TOC, index) on December 10, 2008 and available at lulu storefront.

Subscribe to the blog via a reader or email to stay updated with this blog. **Let the information follow you**.

### Like this:

Like Loading...

## Published by

### Autar Kaw

Autar Kaw (http://autarkaw.com) is a Professor of Mechanical Engineering at the University of South Florida. He has been at USF since 1987, the same year in which he received his Ph. D. in Engineering Mechanics from Clemson University. He is a recipient of the 2012 U.S. Professor of the Year Award. With major funding from NSF, he is the principal and managing contributor in developing the multiple award-winning online open courseware for an undergraduate course in Numerical Methods. The OpenCourseWare (nm.MathForCollege.com) annually receives 1,000,000+ page views, 1,000,000+ views of the YouTube audiovisual lectures, and 150,000+ page views at the NumericalMethodsGuy blog. His current research interests include engineering education research methods, adaptive learning, open courseware, massive open online courses, flipped classrooms, and learning strategies. He has written four textbooks and 80 refereed technical papers, and his opinion editorials have appeared in the St. Petersburg Times and Tampa Tribune.
View all posts by Autar Kaw

I think it would be a good idea to add the possibility of removing a set of numbers, that way you can remove last week’s winning combination.

LikeLike

Picking random numbers each time you play the lottery seems an odd thing to do.

Accoring to Lottery management companies, 80% of the people pick random numbers 20% choose their own and play the same numbers each time.

Which group get better results? Apparently the split is exactly 50/50. So choosing your own numbers and sticking with them seems to be a better option.

Be careful though to choose a set of numbers that have never won before, because the chances of the same set of numbers winning twice, especially with the 5 and 6 picks, is highly improbable as to make it considered impossible.

LikeLike

You should be careful picking the same numbers evrey time though because that forces you to play every week. How would you feel if you missed it once and that was the time “your numbers” came up?

Also I’m not sure I follow the reason that you shouldn’t pick numbers that have come up before> I understand it is unlikely that a set of numbers will appear twice but the probability of certain numbers coming up one week is the same whether or not they came up the week before isn’t it.

LikeLike

………here’s the deal 🙂

Picking RANDOM NUMBERS is INSANE!!!

Using the frequencies of the most picked numbers, in a certain time of the week, is probably the best you can do to win a lottery!

Wayne

LikeLike

I do not think there is any science to picking lottery number. Nothing is truly random but all the schemes people come up with choosing lottery numbers are not correct.

LikeLike

I am having trouble viewing this blog. It displays all funny with text displaying incorrectly. You might want to look at this.

LikeLike

Try using internet explorer.

LikeLike

The numbers evolve and reproduce like the nature, the lotto numbers are also have some forms genetical patterns due to the nature of the system. Thus if you could find out the genetical patterns of the drawings and unless a mutation occurs the system nature forces a condition of survival of the fittest so you have the numbers.

LikeLike

Sir,

your commands generate only one lines of 6 numbers

how do I generate more lines ?

pardon me am not familiar with math lab just got it to try my luck.

LikeLike