Picking lotto numbers

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?

% 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
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(‘  ‘)
% xlow= lowest integer allowed
% xhigh = highest integer allowed
% number of integers to be picked

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.
while (i<=m)
% flag= variable that keeps track of previous numbers
% being same or different from the number picked
for j=1:1:i-1
if (lottonum(i)==lottonum(j))
if flag==0
disp(‘ ‘)
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.


Author: 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.

12 thoughts on “Picking lotto numbers”

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


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


  2. ………here’s the deal 🙂


    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!



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


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


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


Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

%d bloggers like this: