The Lottery

barbie on 2003-06-25T11:59:14

After watching Nicole's brother disappointment when the lottery numbers came up on Saturday, I thought I'd write a prediction script. It does two predictions, the first is based on all the previous draws and the second is based on random numbers.

Not too surprisingly it showed that predictions based on previous draws gave a better return than the random predictions, and that both paid out less than the cost of the predictions.

The initial script was based on upto 10 lines each per draw. Eliminating duplicates (numbers in lines and lines themselves) sometimes reduced to less than 10. There have been over 700 draws so far to work with, so not a bad amount of sample data. I'm trying some tweaks, but I still think you only win by not playing.

However, I think I won as I got to play with and understand pack() better and used The Guttman-Rosler Transform to speed up the sorting process.

Now I just need to figure a quick way of calculating the number of unique characters in a given string.

Currently reading Dave Cross' Data Munging With Perl, which is proving a nice refresher to stuff I haven't done in a while.


number of unique characters

vsergu on 2003-06-25T13:00:39

I'm still a bit in golf mode, but here's a possibility:
$uniq_chars = keys %{{ split /()/, $string }};

Re:number of unique characters

barbie on 2003-06-25T15:26:49

Odd number of elements in anonymous hash at uniq.pl line 5.

It doesn't like having an odd number of characters in the string length. Looks like the basis for what I was after, so some tweaking might be in order.

Thanks for the suggestion.

Re:number of unique characters

vsergu on 2003-06-25T15:58:00

Oops, I forgot to turn on warnings when I ran the one-liner (told you I was in golf mode!). Actually it gives the warning even if the string has an even number of characters. Here's a variation:
$uniq_chars = keys %{{ $string =~ /(.)()/gs }};

Re:number of unique characters

barbie on 2003-06-25T17:52:34

I thought I'd do a bit of benchmarking and was rather surprised at the results.

This was the code:

#!/usr/bin/perl -w
use strict;

use Benchmark qw(:all);

my $string = "ABCDEEsdasdasdfsadfwewasdaSD";

cmpthese(100000, {
    'foreach' => \&do_foreach,
    'map' => \&do_map,
    'split' => \&do_split,
});

sub do_foreach {
    my %hash;
    foreach ( split //, $string ) { $hash{$_}=1; }
    my $uniq_chars = keys %hash;
}

sub do_map {
    my %hash;
    map {$hash{$_}=1} split //, $string;
    my $uniq_chars = keys %hash;
}

sub do_split {
    my $uniq_chars = keys %{{ $string =~ /(.)()/gs }};
}
With the following results:
           Rate   split foreach     map
split   10000/s      --    -15%    -20%
foreach 11830/s     18%      --     -5%
map     12475/s     25%      5%      --
The map snippet was what I was using, so it looks like I made the right choice. Always good to see other ways of doing it. So it was still a useful exercise.

Re:number of unique characters

vsergu on 2003-06-25T19:24:26

I admit mine was a bit obfuscated. You might try this one in your benchmark:
sub do_slice {
    my %hash;
    @hash{ split //, $string } = ();
    my $uniq_chars = keys %hash;
}

I don't understand

sheriff_p on 2003-06-26T08:38:16

I'm probably being exceptionally silly, but why would predictions based on previous draws give you better results than random numbers?

Re:I don't understand

barbie on 2003-06-26T21:47:38

Theoretically they shouldn't, but if there is a set of numbers or even a sequence of numbers that persistantly appear in draws, then using them seems to have a better chance of winning small prizes.

In all the test runs I've done so far there has only one prediction that has made a bonus+5 win, and that was from the predictions based on previous draws.

It doesn't prove anything and I only wrote it to wile away the hours on a Sunday. But it did prove useful for learning about the sorting and other alogrithms.

Re:I don't understand

malte on 2003-06-28T08:30:47

It doesn't. The only thing you can maximize by analyzing previous drawings is your possible win (by choosing numbers which are not frequently played by your fellow players).

Re:I don't understand

chaoticset on 2003-07-12T03:11:26

...waaaait, wouldn't that only be true if there were no pattern in the numbers?

Re:I don't understand

malte on 2003-07-13T15:29:58

Patterns - Yes... but not in the numbers that are drawn but in the numbers which are chosen by other players.

Re:I don't understand

chaoticset on 2003-07-13T17:37:49

...so even if there's a pattern in the numbers being drawn, it's irrelevant to trying to win more? Why wouldn't finding that pattern produce increased winnings?

This can't be the Monty Hall problem...what's the deal?