Win a copy of Kotlin in Action this week in the Kotlin forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

coin flip program  RSS feed

 
micah wise
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ok so I am a newbie to java coding and I have an assignment to do a coin flip program. No problem there, except he also wants the program to skew the results. So, in the coding we have to add a ratio or some coding to improve the chance of either heads or tails??? Help?@!?
 
Jeanne Boyarsky
author & internet detective
Sheriff
Posts: 37234
519
Eclipse IDE Java VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Michah,
A ratio (or percentage) is a good way to think of it. How are you getting the results when it is 50/50?
 
micah wise
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I feel slightly dumber than my IQ but I am not really sure, I have tried changing numbers around within the coding and it has not changed anything, thanks ever so much for the help!....



 
Jeanne Boyarsky
author & internet detective
Sheriff
Posts: 37234
519
Eclipse IDE Java VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I tried changing your .5 to .95 to make it a really skewed coin. See this ideone example.

The result was:
Number of Heads = 92
Number of Tails = 8

What do you see not working in yours?
 
micah wise
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Got it!!! Created a whole new class and bingo....I haven't a foggy clue what was really wrong with the other just now but creating a whole new class and creating from the beginning did it!! Thanks ever so much, you confirmed that my code was likely right but something else was a miss so I went digging! My next assignment is about family probability and whether there will two boys, boy/girl or two girls. Do you think you might be able to help, I would sure appreciate it in bunches! I swear my IQ is just fine but this stuff has me just a little batty trying to get used to it and learning it!
 
Ganesh Patekar
Bartender
Posts: 696
23
Eclipse IDE Hibernate Java jQuery MySQL Database Netbeans IDE Oracle Spring Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to CodeRanch!

Glad to hear you got the solution , can you please post the solved code so It may also be helpful to others who perhaps have same problem.

Of course you can create a separate topic for your new problem, volunteers who knows the answer will definitely try to help you.
 
micah wise
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ok so he isn't preferential of the boolean method what other way is there to represent 50 : 50 or 60 : 40 or etc??? I am really lost
 
Knute Snortum
Sheriff
Posts: 4073
112
Chrome Eclipse IDE Java Postgres Database VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
micah wise wrote:Ok so he isn't preferential of the boolean method

Who said this?  What "boolean method"?
what other way is there to represent 50 : 50 or 60 : 40 or etc??? I am really lost

Me too!  Could you post your code with a comment on the line(s) that are the problem?
 
Junilu Lacar
Sheriff
Posts: 11144
160
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
micah wise wrote:Ok so he isn't preferential of the boolean method what other way is there to represent 50 : 50 or 60 : 40 or etc??? I am really lost


Here's what I'm guessing you mean by this:  he prefers you to use a different method other than (rand < skewPoint) where you change the value of skewPoint to skew the results. 

If so, consider this:
 
Piet Souris
Rancher
Posts: 1979
67
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
That prof has a strange sense of humor in that case.

@micah
an experiment that has two possible outcomes (success/failure, 1/0, true/false, et cetera) is called a Bernoulli trial. It is characterized by one number p with 0 <= p <=1, the chance of success. A straightforward way to  model this is for instance:

But sometimes it is more convenient to return either a 1 or a 0, like in

and I suppose that this is what the prof meant.

With this latter definition  your flip con program becomes as simple as

There is nothing to stop one from repeating many trials in succession. If we are interested in the total number of successes,  then this total is according to the so called Binomial distribution. This is exactly what you do in your flip coin code . Applications are plenty:

If we have 1000 children, what is the chance  that we  have more than 550 girls
if we cast 100 dice, what is the chance that 30 of 'm have either 2 or 5 eyes
if we throw a coin 1000 times and we get 650 times a head, do we still  believe that head has a 50% chance?

I read that your next assignment is about  boys and girls. Let me finish with a small classic problem:

A family has two children. What is the chance that both of these are boys, if we know that
* the older child is a boy
* at least one of them is a boy?

Happy coding.
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!