Monday, October 5, 2009

Re: A Question for all the Java Jockeys Out There

It probably has to do with the way you're declaring and constructing
your instance of java.util.Random inside the static method randomInt
in your gatling class.

Basically, every time you run randomInt you're constructing a new
Random object but with no seed. Java uses a default seed that, I
think, is something like a millisecond timestamp. BUT since in a
simulation like this, you're running randomInt multiple times per
milisecond, it means that Random is getting seeded with the same value
multiple times in a row.

To avoid this you should really only use one instance of Random for
any particular of the program. So your gatling class should be
something like:

public class gatling{

// instance of Random to use whereever you need it
// will be constructed the first time a static method is called on
the gatling class, and then not again.
private static Random myRand = new Random();

public static int randomInt(int lo, int hi){
return myRand.nextInt(....)
//etc.
}

public static char randomChar(){

return (char)( myRand.nextInt(26))+'a'
}

//etc.
}


--------------------------------------------------------------
Baker Franke
Computer Science Dept.
The University of Chicago Laboratory Schools
773.702.5419

On Oct 5, 2009, at 3:13 PM, Keith E Gatling wrote:

> Last week my class discussed the statistical question of what the
> likelihood
> was of a family with two kids having both of them be girls. We knew
> that it
> was supposed to be 25%, but the programs that my students wrote all
> came up
> with likelihoods in the area of of 40%, and this was after multiple
> runs
> with sets of 100, 1000, and even 30,000,000 families.
>
> I decided to find out what they were doing wrong by writing the code
> myself
> (without looking at theirs) and checking for the correct proporotion
> of boys
> and girls in each column. It worked like a charm - except that even
> I got
> the skewed stats of roughly 40% for two-girl families. In fact, I was
> getting roughly 40% for families with both two girls and two boys,
> while
> families with one of each were roughly 20%
>
> I checked this out with my sister the statistician, who said that my
> logic
> was correct, and then wrote it herself in SAS. She got the expected
> figures.
> But we're all still a bit confused as to why we keep getting the
> right stats
> for each column (Kid A and Kid B), but totally wrong stats for BB,
> BG, and
> GG.
>
> Do any of you Java experts out there want to take a look at my code to
> either try to figure out what we're doing wrong, or maybe run it in a
> different environment to see if you get different results? We've
> been doing
> it in BlueJ on Macs. Do you get different results in BlueJ in
> Windows or
> Linux? Do you get different results in a different programming
> environment?
>
> The main code in question is at:
> http://www.gatling.us/keith/class/javajive/TwoKids.java
> The gatling class it refers to for gatling.randomInt is at
> http://www.gatling.us/keith/class/gatling.java
> For what it's worth, the gatling.randomInt method is merely an
> interface to
> the standard Java method of creating random numbers.
>
> Can anyone solve this conundrum for us?
>
> Thanks!
> --
>
> keg
>
> ========================================
> Keith E Gatling
> Email: keith@gatling.us
> Blog: http://wordfromg.blogspot.com
> Website: http://www.gatling.us/keith
> The fact that I'm open-minded doesn't mean that I have to agree with
> you.
> ========================================
>
> [ For info on ISED-L see http://www.gds.org/ISED-L ]
> Submissions to ISED-L are released under a creative commons,
> attribution, non-commercial, share-alike license.
> RSS Feed, http://listserv.syr.edu/scripts/wa.exe?RSS&L=ISED-L
>

[ For info on ISED-L see http://www.gds.org/ISED-L ]
Submissions to ISED-L are released under a creative commons, attribution, non-commercial, share-alike license.
RSS Feed, http://listserv.syr.edu/scripts/wa.exe?RSS&L=ISED-L