?

Log in

No account? Create an account
 
 
13 May 2004 @ 11:12 am
 
/**

1. An integer, seed, to start the random number generator (just as it was done for program 3 - InBetween).
2. An integer, numSharing, specifying how many people must share the same birthday in order to consider a trial successful.
3. An integer, maxPeople, specifying the maximum number of people "in the room".
4. An integer, numTrials, specifying the number of trials to run for each number of people.



psudo code:
create an array representing the days of the year
add birthdays
check to see if any day has more than one birthday in a day.
compute the probability of the having n people born on the same day out of the sample.


one trial:
create the array:
make a new array of 365 ints.
possibly initilize all to 0.
find the number of people. int maxPeople = [the number of people];
randomly add 1 to maxPeople number of days.
see how many birthdays are shared by numSharing
repete the trial numTrials times.

the math test
-------------------
*/



import java.util.Random;
//import tio.*;

class birth
{

public static void main(String[] args) {
System.out.print("What is maxpeople:");
int maxPeople = 300; //Console.in.readInt();
trial(maxPeople);
System.out.println("That was hella rockin!");

}

static void trial(int maxPeople)
{
int[] theYear = new int[365];
for(int mp = 0; mp < maxPeople; mp++)
{
int oneGuy = random.nextInt(365);
theYear[oneGuy] = theYear[oneGuy] + 1;
}

for(int i = 0; i < theYear.length; i++)
{
System.out.print( theYear[i]);
if (theYear[i] > 1)
{
System.out.println();
}
}
}






/**
This method returns a new Random (random number generator object).
If the array args is not empty, the first element is assumed to be
a string representation of an integer. The integer will be used to
"seed" the random number generator so that it generates a repeatable
string of pseudo-random numbers.
If the array args is empty, the random number generator is created
with a random seed (internally by the class Random).
@param args - an array that is either empty or starts with a string
representation of an integer - used to seed the random number
generator
@return a newly created (and seeded) random number generator
*/
static Random initializeDice(String[] args) {
if (args.length == 0) {
return new Random();
}
else {
return new Random(Integer.parseInt(args[0]));
}
}
}
 
 
 
Siner Dsirnerd on May 13th, 2004 10:09 pm (UTC)
cout <<"i'm a nerd";
the subliminal messagereynaldo77 on May 13th, 2004 10:35 pm (UTC)
String $feeling = "hate";
System.out.println('I ' + $feeling + ' Java.');
steakalicious: armquinnc on May 18th, 2004 09:30 pm (UTC)
import tio.*;
import java.util.*;

class Birth2 {

int firstArg;
if (args.length > 0)
firstArg = Integer.parseInt(args[0]);


public static void main(String[] args) {
Random random = initializePeople(args);
int seed , numTrials, numSharing, maxPeople;
float trueTrials = 0.0;
if (args.length == 4) {
seed = Integer.parseInt(args[0]);
numSharing = Integer.parseInt(args[1]);
numPeople = Integer.parseInt(args[2]);
numtrials = Integer.parseInt(args[3]);
}
else{ System.out.println("Inscrutible Error -2222B./n");}

/** // This was for testing. now it reads from the command line
System.out.println("What is maxpeople:");
maxPeople = Console.in.readInt();
System.out.println("What is numSharing:");
numSharing = Console.in.readInt();
System.out.println("What is numTrials:");
numTrials = Console.in.readInt();
//trial(maxPeople, args);
*/


for (int i = 1; i <= numTrials; i++) //this runs the trials
{
trueTrials = (trial(maxPeople, args, numSharing)) + trueTrials;
}
System.out.println( "The probability of " + numSharing " people sharaing a birthday with a sample pool of " + numPeople + " people is " + (trueTrials / numTrials) + " after " + numTrials + " trials.");
}



static int trial( String[] args )
{
numSharing = Integer.parseInt(args[1]);
numPeople = Integer.parseInt(args[2]);
numtrials = Integer.parseInt(args[3]);
Random random = initializePeople(args);
int[] theYear = new int[365];
for(int mp = 0; mp < numPeople; mp++)
{
int oneGuy = random.nextInt(365);
theYear[oneGuy] = theYear[oneGuy] + 1;
}

for(int i = 0; i < theYear.length; i++)
{
System.out.print( theYear[i]);
if (theYear[i] >= numSharing)
{
System.out.println();
System.out.println("greater than numSharing!");
return 1;
}
}

for(int i = 0; i < theYear.length; i++)
{
System.out.print( theYear[i]);
if (theYear[i] > 1)
{
System.out.println();
}
}
System.out.println("end of trial. fail.");
return 0;
}






static Random initializePeople(String[] args) {
if (args.length == 0) {
return new Random();
}
else {
return new Random(Integer.parseInt(args[0]));
}
}
}