Dear Monks,
I wrote a script to calculate the propability to exceed a defined pips sum throwing a loaded die with x sides n times. The number of pips for each side can also be defined.
$throw_die_ntimes=5; $min_summed_pips=60; $sides_of_die=5; # propability to get a certain side @propabilities=(0.05,0.1,0.2,0.3,0.35); # number of pips for each side @values=(1,5,7,13,17); # generate possible compositions @start_array=("1","2","3","4","5"); foreach(1..$throw_die_ntimes-1) { %hash=(); foreach$composition(@start_array) { foreach(1..$sides_of_die) { $new_composition=$composition.$_; @sort_new_composition=split('',$new_composition); @sort_new_composition=sort@sort_new_composition; $new_composition=join('',@sort_new_composition); push(@new_array,$new_composition); $hash{$new_composition}++; } } @start_array=@new_array; @new_array=(); } # calculate propability of each composition $total_prop_to_exceed_minimum=0; $total_prop_to_deceed_minimum=0; foreach$composition(keys%hash) { $propability_of_composition=1; $value_of_composition=0; @composition=split('',$composition); foreach$step(@composition) { $propability_of_composition=$propability_of_composition*$propa +bilities[$step-1]; $value_of_composition+=$values[$step-1]; } $propability_of_composition=$propability_of_composition*$hash{$com +position}; # add propability to one of two total propabilities (exceed or dec +eed minimum) if($value_of_composition>=$min_summed_pips) {$total_prop_to_exceed_minimum+=$propability_of_composition;} else {$total_prop_to_deceed_minimum+=$propability_of_composition;} } print"p to exceed minimum: $total_prop_to_exceed_minimum\np to deceed +minimum: $total_prop_to_deceed_minimum\n\n"; system("pause"); exit;
This code works but gets very slow when I throw the die more then 10 times. The problem seems to be the exploding array when calculating all possible compositions. I pored over a smarter way to do this but still I do not have a clue.
Any ideas?

In reply to Loaded die by Microcebus

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.