SPUG: Stick Riddle

Michael R. Wolf MichaelRunningWolf at att.net
Thu Jan 2 01:33:50 CST 2003


Damian Conway <damian at conway.org> writes:

[code elided...]

Using Damain's code as a base, I changed two assumptions.

 * I always break the left-most (not longest) piece on the second break.

 * The second break point is independent of the first break point.

Here's the modified code, which yeilds legal triangles 49.592% of the
time.

================================================================

#! /usr/bin/perl -w

sub norm_rand {
    my $norm = 0;
    for (1..8) { $norm += rand() }
    return $norm / 8;
}

foreach (0 .. 100_000) {

    # Break stick in two...
    my $first_break_at = norm_rand();
    my @piece = ($first_break_at, 1-$first_break_at);

    # Break next piece in two...
    my $second_break_at = norm_rand();
    @piece[1,2] = ($piece[1]*$second_break_at, $piece[1]*(1-$second_break_at));

    # Do they form a triangle?
    @piece = sort { $a <=> $b } @piece;
    $total_tris++ if $piece[2] < $piece[1] + $piece[0];
    $total_tries++;
}

print "Percent that made triangles: ", $total_tris/$total_tries * 100;



-- 
Michael R. Wolf
    All mammals learn by playing!
        MichaelRunningWolf at att.net


 - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
     POST TO: spug-list at pm.org       PROBLEMS: owner-spug-list at pm.org
      Subscriptions; Email to majordomo at pm.org:  ACTION  LIST  EMAIL
  Replace ACTION by subscribe or unsubscribe, EMAIL by your Email-address
 For daily traffic, use spug-list for LIST ;  for weekly, spug-list-digest
     Seattle Perl Users Group (SPUG) Home Page: http://seattleperl.org




More information about the spug-list mailing list