lotto - pick n numbers from range

Submitted scripts and programs
Forum rules
Your own work only.
machinebacon
Baconator
Posts: 10253
Joined: Thu Sep 16, 2010 11:03 am
Location: Pfälzerwald
Contact:

lotto - pick n numbers from range

Unread post by machinebacon » Wed Feb 03, 2016 8:32 pm

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

User avatar
simgin
Meme Fodder
Posts: 1167
Joined: Sun Jan 06, 2013 12:07 am
Location: Bradford-on-Avon, UK

Re: lotto - pick n numbers from range

Unread post by simgin » Wed Feb 03, 2016 10:10 pm

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
Someone told me that I am delusional, I almost fell off my unicorn.

machinebacon
Baconator
Posts: 10253
Joined: Thu Sep 16, 2010 11:03 am
Location: Pfälzerwald
Contact:

Re: lotto - pick n numbers from range

Unread post by machinebacon » Thu Feb 04, 2016 6:59 am

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

User avatar
wuxmedia
Grasshopper
Posts: 6445
Joined: Wed Oct 17, 2012 11:32 am
Location: Back in Blighty
Contact:

Re: lotto - pick n numbers from range

Unread post by wuxmedia » Thu Feb 04, 2016 7:30 am

Bumper sticker - "Perl scripters do it with chomp"
"Seek, and Ye shall find"
"Github | Chooons | Site"

User avatar
simgin
Meme Fodder
Posts: 1167
Joined: Sun Jan 06, 2013 12:07 am
Location: Bradford-on-Avon, UK

Re: lotto - pick n numbers from range

Unread post by simgin » Thu Feb 04, 2016 9:38 am

^^ Aha, thank you Julius that was very descriptive.

^ hehe, wuxie :D
Someone told me that I am delusional, I almost fell off my unicorn.

Post Reply