Page 1 of 1

lotto - pick n numbers from range

Posted: Wed Feb 03, 2016 8:32 pm
by machinebacon
If you like to play something like Pick 6 lottery you can use this to generate the winning numbers. Remember to send a beer to all users if you win.

Code: Select all

#!/usr/bin/perl
use strict;
use warnings;

print "How many maximum numbers? ";
my $max_number = <STDIN>;

print "How many random numbers to pick? ";
my $pick_number = <STDIN>;

chomp $pick_number;
chomp $max_number;
print "Picking $pick_number of $max_number numbers.\n";

for (1..$pick_number) {
    my $range = $max_number;
    my $randomized_number = int(rand($range)) + 1;
    print $randomized_number . "  ";
}   

print "\n";
TODO: don't pick the same number twice

Re: lotto - pick n numbers from range

Posted: Wed Feb 03, 2016 10:10 pm
by simgin
Hmm, I wish there was a way to find out their algorithms, or the method of how the winning numbers are actually found. Ah well, man can only dream.

But thanks for the the script Julius, stuff like this makes it more fun to learn programming. Good idea, very educational.
If i understood exactly what it was doing, then I can use it as an exercise for my learning of Python (rookie here) Just to see how it would look like in an other language. Hence I am browsing all the fantastic scripts in this forum :)


cheerio
simon

Re: lotto - pick n numbers from range

Posted: Thu Feb 04, 2016 6:59 am
by machinebacon
You mean how the lottery picks the numbers? Traditionally: they have a bucket with <max_number> number of balls, each numbered from 1 to <max_number>, and they pick <pick_number> times more or less manually. Some variants pick an additional <pick_number+1> from a separate bucket with only 9 balls to make the odds higher.

The script asks for two inputs, <$max_number> is a range (I could have used $max_number as $range directly, but I wasn't sure if it counts from 0 or 1 - you can actually ask directly for <$range>) and <$pick_number> is the count for the loop. The "chomp" command just removes the newline character at the end of the user input, you can compare how the output looks like if it is "print"ed without "chomp". The for-loop is actually C-style, or same as in bash. The randomizer picks an integer: a number from the $range +1, to prevent zero. After the loop I just add a newline character to make the output a bit cleaner.

Re: lotto - pick n numbers from range

Posted: Thu Feb 04, 2016 7:30 am
by wuxmedia
Bumper sticker - "Perl scripters do it with chomp"

Re: lotto - pick n numbers from range

Posted: Thu Feb 04, 2016 9:38 am
by simgin
^^ Aha, thank you Julius that was very descriptive.

^ hehe, wuxie :D