You are creating a long ruler, with physical lengths marked off according to the weight of each ship's initiative. You throw a stone randomly on the ruler, and whoever owns the area it lands in, wins the draw. That is a fine algorithm, but here is one might suit you better.

You, me, and Dupree each have ships - yours is 25 powerful, mine is 15, and Dupree's is 60.

  1. Make index cards for each of us, writing our names and power levels on each card.
  2. Total up all the power levels, and stack up the cards.
    (Conveniently, it adds up to 100, but this works with any total.)
  3. Roll a 100-sided (or whatever_the_total_is-sided) die. This time, we roll 41.
  4. Leaf through the cards, summing as you go. When your sum exceeds your rolled number, stop on that card.
    • No card - 0; sum = 0; less than 41 -> continue
    • You - 25; sum = 25; less than 41 -> continue
    • Me - 15; sum = 40; less than 41 -> continue
    • Dupree - 60; sum = 100; greater than 41 -> Stop! Dupree wins the draw.

Note that this technique works with real (non-integer) values (we do not round off the result of rand()), and works without having to sort the cards. Sorting in a descending order could yield a *slightly* faster search, depending on the distribution of values. In fact, if you are building the card deck few enough times, and scanning it many times, and never changing the values, you can speed it up even more by writing the running total on each card. This does not match your use-case, though.

I rely on other, less-sleepy monks to warn if I have an off-by-one error, or have completely botched the weighted implementation.
Completely untested code:

use List::Util qw( sum ); sub simulate_mayhem { my @shipsInCombat = @_; # Initiative levels for each ship. # This is a parallel array to @shipsInCombat. my @ship_initiatives = map { $_->{thruster} * 2 + 1; } @shipsInCombat; while ( my $total_initiative = sum @ship_initiatives ) { my $chosen_initiative = rand $total_initiative; my $initiative_sum = 0; # Required - can't use the loop var after the loop # See PBP 6.9. Non-Lexical Loop Iterators my $chosen_ship_number; for my $ship_number ( 0 .. $#shipsInCombat ) { $initiative_sum += $ship_initiatives[$ship_number]; next if $initiative_sum <= $chosen_initiative; $chosen_ship_number = $ship_number; last; } die "Can't happen" if not defined $chosen_ship_number; my $chosen_ship = $shipsInCombat[$chosen_ship_number]; ship_does_something($chosen_ship); # Now reduce that ship to 0, so it cannot be picked again. $ship_initiatives[$chosen_ship_number] = 0; # If you want, you can recalculate {thruster} on any ship # affected by ship_does_something(), as long as you have a # way of tracking the ones that had used their initiative, # and force it back to 0. ### $ship_numbers_that_used_initiative{$chosen_ship_number} = +1; ### recalc_thrusters(\@shipsToAct); ### @ship_initiatives = map { ### $_->{thruster} * 2 + 1; ### } @shipsInCombat; ### ship_initiatives[$_] = 0 for keys %ship_numbers_that_used_ +initiative; } } # Loop exits when all ships have 0 initiative.

In reply to Re: Rolling For Initiative by Util
in thread Rolling For Initiative by SuicideJunkie

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.