My colleagues want to organize a small "community" to play Superenalotto, an Italian lotto game. They are going to try all the 90 possible numbers in 15 columns of six numbers every week. Can perl make us Euro-millionaires? Let's give it a try!

Update: used qq instead of double quotes in print (thanks to Roy Johnson), but can't use double quotes for the -e option because the bash shell gets into the argument before perl (tips?); substituted splice(@n,rand($#n) with splice(@n,rand(sprintf('%2.0f',$#n))

Update: actually, this code will almost always leave the 90 for last (see perldoc -f rand); this one should work better

perl -e '@n = (1..90) ; my @colonne ; while (@n) { my @colonna ; for ( +1..6) { push @colonna,splice(@n,rand($#n+1),1) } ; push @colonne,\@co +lonna } ; foreach my $c (@colonne) { print qq(@$c\n) }'

Replies are listed 'Best First'.
Re: Try your luck at Superenalotto!
by Roy Johnson (Monsignor) on Apr 28, 2004 at 19:59 UTC
    Adjust quotes as appropriate if not on a PC.
    perl -M"List::Util 'shuffle'" -e "my @n=shuffle(1..90); print join(',' +, @n[$_*6..$_*6+5]), qq(\n) for (0..14)"

    The PerlMonk tr/// Advocate
Re: Try your luck at Superenalotto!
by thor (Priest) on Apr 28, 2004 at 22:01 UTC
    Just for grins, I calculated the odds...I think it's right! The stats given are for getting exactly that many numbers correct.
    1. 235054512 in 622_614_630
    2. 33388425 in 622_614_630
    3. 2046800 in 622_614_630
    4. 53550 in 622_614_630
    5. 504 in 622_614_630
    6. 1 in 622_614_630
    To get the stats for at least that many numbers correct, add up all the results below the number in question. For instance, to get at least two numbers, you'd add up the stats for 2, 3, 4, 5, and 6 numbers correct.

    thor

    Update: In case anyone cares, the formula I used was choose(6,n)*choose(90-n-1,6-n)/choose(90,6) where choose(n,r) is defined as n!/(r!(n-r)!).
      Shouldn't the odds of getting at least one right add up to 622_614_630?

      -QM
      --
      Quantum Mechanics: The dreams stuff is made of

        If I understand your question: no. There are a whole lot of opportunities to get none right, as is evidenced by my own lottery playing experience...:)

        thor

Re: Try your luck at Superenalotto!
by perlinux (Deacon) on Apr 29, 2004 at 08:24 UTC
    Great one-line application!! :-)
    This is a Perl-solution for my (wednesday | saturday) problems!!
    ++

    Italian:
    Almeno non dovrò star lì a pensare quali numeri giocare!!
Re: Try your luck at Superenalotto!
by QM (Parson) on Apr 30, 2004 at 02:13 UTC
    Now that you've done that, can you create a set to cover all pairs of numbers? Triplets? How would that improve the odds?

    -QM
    --
    Quantum Mechanics: The dreams stuff is made of

      In for (1..6) change 6 with 2 or 3. How this would improve the odds, I don't know :-)

      Ciao!
      --bronto


      The very nature of Perl to be like natural language--inconsistant and full of dwim and special cases--makes it impossible to know it all without simply memorizing the documentation (which is not complete or totally correct anyway).
      --John M. Dlugosz