# Java program help

Discussion in 'OT Technology' started by SiempreFresco, Oct 11, 2005.

1. ### SiempreFrescoNew Member

Joined:
Jun 16, 2003
Messages:
7,142
0
Location:
NYC
I need to write a program that would simulate a weighted coin toss between two people, one guy heads one guy tails. During the first two flips have a 55% chance of heads, the next 3 will have a 48% chance of heads, the last two flips have a 55% chance of heads. The first guy to get 4 tosses in their favor wins the toss.
I need to simulate the toss 200 times and estimate the probability that heads will win.

Then I need to run additional simulations and estimate the probability that the guy who is head wins in 4 tosses, 5 tosses, 6 tosses, and 7 tosses.

And I need to run additional simulations and estimate that the winner is decided, regardless of who wins, in 4 tosses, 5 tosses, 6 tosses, and 7 tosses.

I need help, stat

2. ### LogosNew Member

Joined:
Feb 23, 2004
Messages:
3,220
0
Location:
U-Dub
It'd be pretty easy to hard code in. Just use the random number generator to come up with a number between 0.0 and 0.999... For the first toss, see if it's <0.55. If so, heads wins, if not, tails. For the third toss, if it's <0.48, heads wins, if not, tails. etc.

Keep two counters for each set of tosses and increment the proper one if heads or tails wins. Test the counters each time, and when one gets up to four, proclaim a victor.

Use a second set of counters to keep track of the victors. When you've run it 200 times, the probability of heads winning is just #victories/#games. Same for tails.

3. ### SiempreFrescoNew Member

Joined:
Jun 16, 2003
Messages:
7,142
0
Location:
NYC
how would I limit the the 55% chances to just the first two flips and last two flips?

4. ### PeyompNew Member

Joined:
Jan 11, 2002
Messages:
14,017
0
Make a function in your class that changes the value of the odds. setOdds(int) and getOdds(int) ought to do it. Then after two iterations, set the odds differently.

5. ### PeyompNew Member

Joined:
Jan 11, 2002
Messages:
14,017
0
public class Coin {
private int odds;
private boolean result;

public void setOdds(int odds) { this.odds = odds; }

public int getOdds(){ }

/*** 0 is heads, 1 is tails ***/
public boolean doFlip(){ }

}

Alternatively, you can just have doFlip take an arguement, like this:

boolean doFlip(int odds) { }

I leave the implementation up to you.

Last edited: Oct 11, 2005
6. ### SiempreFrescoNew Member

Joined:
Jun 16, 2003
Messages:
7,142
0
Location:
NYC
I still need help, what i have so far:

public class CoinSim
{
public static void main(String[] args)
{
int games_won = 0;
int tails = 0;
int tails = 0;
for (int i = 0; i < 1000; i++)
{
games_won = (int)(Math.random() +.55);
if (games_won > .55)
{
}
else
{
tails++;
}

System.out.println("tails : "+ tails);
}
}

I'm stuck I have no clue how to print out the probability or how to get the program to stop the .55 after 2 trials and switch to .48 and then back to .55 and then do that all over again 100 times. Been trying to figure it out for a couple of hours now. If you guys that posted before could elaborate a bit (i'm new at this) I'd appreciate it.

I don't quite understant the get odds thing peyomp suggested where would the .55 go?

Last edited: Oct 11, 2005
7. ### PeyompNew Member

Joined:
Jan 11, 2002
Messages:
14,017
0
I was thinking integers, not floats. 55, not .55, then generate a random number between one and 100, and change the value of the "odds" int.

Look, first things first: CREATE THE OBJECT THAT DOES IS THE THING YOU WANT. Don't just do this in main. That sucks. Make a coin object. Then make another object that uses it in its own main function. Doing so makes your thinking, and your code clearer.

Okay, don't use ints. use doubles.

// You are creating a COIN, a THING, a type of OBJECT that can do things... like being tossed
public class Coin {

...// A function performs an action, in this case a toss of our coin
...public boolean Toss(double odds) {

......// Get your number between 1 and zero
......double rand = Math.rand();

......// Now evaluate whether that random number is less than, or greater than or equal to your odds

......// Now return a boolean, depending on the result of our last step.

...}

}

Fill this class out, complete it, and then make another class that calls it. And lookup 'encapsulation' in your textbook to understand why.

8. ### PeyompNew Member

Joined:
Jan 11, 2002
Messages:
14,017
0
Once you have your coin class created, then you look at the next step in your problem... how to represent a series of tosses?

Think about that last sentence. "how to represent a series of tosses?" Why... with a type of OBJECT: in this case, a "SERIES" object. You can either generalize this and make it configurable... or just make it do the right sequence of tosses, so that you can then use it to perform many sequences in a row.

See how Object Oriented Analysis and Design makes it easier? Instead of tryign to figure out an algorithm (they are HARD at first!) to get it done in a procedural main() funciton, why not use OO and make the problem MUCH SIMPLER.

To state your problem clearly, we have a coin (object) which gets tossed (behavior) repeatedly in many repeating series (object).

So we need a Coin class, which is used by our Series class. And we need a client class which runs our series again and again.

public class Series {

...private Coin coin;

...private int tails;

...private static final double odds55 = 0.55;
...private static final double odds48 = 0.48;

...private boolean result;

...public boolean run()
{
....../*** Insert your logic here. run your tosses for each set of odds, and increment your result ints, heads and tails, depending on the outcome of the different toss operations on your coin object, with the different odds. Depending on who wins (which one is == 4?), return your result. Assign heads or tails 0 or 1 ***/

...}

}

Last edited: Oct 11, 2005
9. ### PeyompNew Member

Joined:
Jan 11, 2002
Messages:
14,017
0
Then the client looks like this:

public class AssignmentClient
{
...public static void main(String[] args)
...{
......boolean result;
......Series hooker = new Series();

......for(int x=0;x<1000;x++)
......{
..........result = hooker.run()
..........// Now look if it was heads or tails and increment the right counter
......}
// And at the end print yo shit.
...}
}

10. ### PeyompNew Member

Joined:
Jan 11, 2002
Messages:
14,017
0
And if u don't thank me... I'll find you, motherfucker.

11. ### SiempreFrescoNew Member

Joined:
Jun 16, 2003
Messages:
7,142
0
Location:
NYC
trust me im grateful

12. ### PeyompNew Member

Joined:
Jan 11, 2002
Messages:
14,017
0
Post your code when you're done.

Do you understand the deal with how to break the problem down into easily understandable chunks, so that you could apply this next time... or did I just do it for you?

13. ### SiempreFrescoNew Member

Joined:
Jun 16, 2003
Messages:
7,142
0
Location:
NYC
I understood most of it, ill post the code later after my classes

14. ### krondoNew Member

Joined:
Oct 5, 2005
Messages:
153
0
Location:
Manhizzle KS
you could've just used a while() loop and had a counter for the number of trials

int trial=1;

while(trial < 3){
the coin toss object goes here
trial++;
}

it would only take 3 while loops and would fit right in with peyomp's coin tossing objects

15. ### PeyompNew Member

Joined:
Jan 11, 2002
Messages:
14,017
0
If you look at his initial attempt... three while/for loopages were going to be too confusing, and he wouldnt' have learned anything. This way, he attacks a problem that is relatively complicated for a beginner with Object Oriented Anlaysis/Design to produce more robust, reuseable code.

Thats the idea, anyway. Too often I see people in here doing assignments using java like its C.

ed. Okay, right. In the Series class.

16. ### FrequencyNew Member

Joined:
Dec 30, 2004
Messages:
7,503
0
Location:
PA
oh i hated when people did that in my classes, so often they would read the problem and code it out right away then ask me for help when it didn't work.

they had so much repetive code(RULE #1!!) their programs were always 3 times longer than mine they never used functions. and when they looked at mine for some hints they always told me it was confusing

17. ### krondoNew Member

Joined:
Oct 5, 2005
Messages:
153
0
Location:
Manhizzle KS
what i meant was that he could do this to call the function you helped him learn how to build, in this case he could just change the variables within the function for each loop...

18. ### PeyompNew Member

Joined:
Jan 11, 2002
Messages:
14,017
0
Yeah, caught that at the end.

19. ### StevesVR4Get Arrested

Joined:
Jul 1, 2003
Messages:
7,328
0
I have one suggestion to improve on this design. Instead of having a hardcoded odds48 and odds55 where you need logic in your run method to figure out which odds value to use, declare an odds array to contain the odds for each of the seven flips. That way, when you loop through the seven attempts, you can just use the next value in the odds array for the proper value instead of needing an if (or switch) statement to figure out which value is the proper one to use. This will make it easier to define and use the appropriate odds value.

20. ### PeyompNew Member

Joined:
Jan 11, 2002
Messages:
14,017
0
I honestly figured he'd just have 7 calls using one of the two parameters, but sure, that would work.

21. ### krondoNew Member

Joined:
Oct 5, 2005
Messages:
153
0
Location:
Manhizzle KS
pure genius, i could use you in my programming class....i need a job like this.

22. ### PeyompNew Member

Joined:
Jan 11, 2002
Messages:
14,017
0
Yes, my ability to compelte textbook problems using Java is why I earn... the... big bucks?

23. ### krondoNew Member

Joined:
Oct 5, 2005
Messages:
153
0
Location:
Manhizzle KS
just interested in being your apprentice...

Joined:
Jan 11, 2002
Messages:
14,017