To LUGNET HomepageTo LUGNET News HomepageTo LUGNET Guide Homepage
 Help on Searching
 
Post new message to lugnet.off-topic.geekOpen lugnet.off-topic.geek in your NNTP NewsreaderTo LUGNET News Traffic PageSign In (Members)
 Off-Topic / Geek / 4185
4184  |  4186
Subject: 
Re: math question (or pattern... whatever...)
Newsgroups: 
lugnet.off-topic.geek
Date: 
Mon, 3 Mar 2003 23:37:58 GMT
Viewed: 
319 times
  
In lugnet.off-topic.geek, Dan Boger writes:
$ perl -le '
$rr = 0;
$count = 0;
for (1..10000) {
$count ++;
@bag = qw/r r w b/;
$hand = "";
for (1..2) {
   $pos = int(rand($#bag));
   $hand .= $bag[$pos];
   splice @bag, $pos, 1;
}
$rr++ if $hand eq "rr"
}
print "$rr/$count = ", $rr/$count;
'
3391/10000 = 0.3391

I don't quite get why this works-- if you're doing rand($#bag), won't that
always pick r, r, or w for the 1st value? Hence, theoretically, you could
pick "wb", which isn't in your sample set... on the other hand, you're
increasing the chance that you'll pick "rr" since you can't pick b first,
and are more likely to pick r. I'm amazed either that this works out by luck
to 33%, or that there's some underhanded secret-math-logic as to why this is
an equivalent situation.

I made a 1st attempt:

--------------
@marbles = ("r","r","b","w");
for(1..1000) {
    $pick = '';
    @m1 = sort { int(rand 3) - 2; } @marbles;
    $pick .= pop @m1;
    $pick .= pop @m1;
    $tot++;
    if($pick =~ /^r/) {
$rfirst++;
if($pick eq 'rr') {
    $rrs++;
}
    } else {
$rnots++;
    }
}
print "$rfirst out of $tot had 1st as red (".($rfirst/$tot).")\n";
print "$rrs out of $rfirst were both red (".($rrs/$rfirst).")\n";
print "$rnots out of $tot were not 1st red (".($rnots/$tot).")\n";
---------------

But notice that this implementation won't work because a random quicksort
won't dramatically alter the order of the array. I modified to something
remarkably similar to yours:

------------
for(1..1000) {
    @marbles = ("r","r","b","w");
    $pick = '';
    for(1..2) {
$n = int rand(@marbles);
$pick .= $marbles[$n];
splice @marbles,$n,1;
    }
    $tot++;
    if($pick =~ /^r/) {
$rfirst++;
if($pick eq 'rr') {
    $rrs++;
}
    } else {
$rnots++;
    }
}
print "$rfirst out of $tot had 1st as red (".($rfirst/$tot).")\n";
print "$rrs out of $rfirst were both red (".($rrs/$rfirst).")\n";
print "$rnots out of $tot were not 1st red (".($rnots/$tot).")\n";
-----------------

Output:

503 out of 1000 had 1st as red (0.503)
169 out of 503 were both red (0.335984095427435)
497 out of 1000 were not 1st red (0.497)

DaveE



Message has 1 Reply:
  Re: math question (or pattern... whatever...)
 
(...) doh, you're right :) Also, I was calculating the chance of getting 2 red marbles, in general, not checking the chance given the first one was red... :) That's what you get when you do an experiment, and have an expected solution in your (...) (22 years ago, 4-Mar-03, to lugnet.off-topic.geek)

Message is in Reply To:
  Re: math question (or pattern... whatever...)
 
(...) I'm not a stats person, but I agree with your friend. r(2) r(1) is the same as r(1) r(2), because order doesn't matter. If it does, then you end up with more combos (w r(2), etc)... -- that's what I was going to say. But, I decided to run a (...) (22 years ago, 3-Mar-03, to lugnet.off-topic.geek)

61 Messages in This Thread:




















Entire Thread on One Page:
Nested:  All | Brief | Compact | Dots
Linear:  All | Brief | Compact

This Message and its Replies on One Page:
Nested:  All | Brief | Compact | Dots
Linear:  All | Brief | Compact
    

Custom Search

©2005 LUGNET. All rights reserved. - hosted by steinbruch.info GbR