Virtual Coin Flip

Started by
27 comments, last by StayFly 11 years, 6 months ago
Could you just have the constructor of your class flip the coin and print the results, and then in your static main member you just create an array of your class with 10 elements? No loops, only a single line of code in main().

Stephen M. Webb
Professional Free Software Developer

Advertisement
Java doesn't have value objects, so creating an array with 10 elements does not create 10 instances of the class.
Thank you for all of the repose everyone. Another thing that i forgot to mention totally sorry is the only place i can use static is in the Public static void main method. Any where else is a No No. Thanks again.


From a performance point of view a function call is more expensive than a comprising.
This piece of code will cause every run to have an additional function call to flip_coin with no result.

Your performance point of view is more or less irrelevant here - performance will be dominated by the I/O performed in this function.


Less LOC's (Lines Of Code) doesn't mean it is more elegant just remember that.
[/quote]
Lines of code does not equate to elegance, of course. That said, I think that by removing an extra comparison alvaro's code is simpler to understand. An alternative that I think would be simpler in your code is to restructure the final condition like so:
[...]
[/quote]

Yes, lines of code have nothing to do with it. I think it's inelegant to check for the termination condition in two places, and the decrement of the passed parameter for no good reason also rubs me the wrong way.

i am trying to make the coin flip 10 times and display how much times heads and tails come out

A loop would be an ideal way, but seeing as you can't use them, why not call 10 "if" statements in a row?

I didn't have a computer available that had Java development stuff on it, so I whipped up a small demo using C# (which is close enough to get the point). I don't know why you can't use a loop, but this is a really poor way of doing it.


using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace flip_example
{
class Program
{
static void Main(string[] args)
{
Random random = new Random();
double start = 0;
double end = 1;
double result = start = (random.NextDouble() * (end - start));
Console.WriteLine(result.ToString());
//1
if (result < 0.5)
{
Console.WriteLine("Head");
}
else if (result > 0.5 && result < 1.0)
{
Console.WriteLine("Tail");
}
//2
result = start = (random.NextDouble() * (end - start));
if (result < 0.5)
{
Console.WriteLine("Head");
}
else if (result > 0.5 && result < 1.0)
{
Console.WriteLine("Tail");
}
//3
result = start = (random.NextDouble() * (end - start));
if (result < 0.5)
{
Console.WriteLine("Head");
}
else if (result > 0.5 && result < 1.0)
{
Console.WriteLine("Tail");
}
//4
result = start = (random.NextDouble() * (end - start));
if (result < 0.5)
{
Console.WriteLine("Head");
}
else if (result > 0.5 && result < 1.0)
{
Console.WriteLine("Tail");
}
//5
result = start = (random.NextDouble() * (end - start));
if (result < 0.5)
{
Console.WriteLine("Head");
}
else if (result > 0.5 && result < 1.0)
{
Console.WriteLine("Tail");
}
//6
result = start = (random.NextDouble() * (end - start));
if (result < 0.5)
{
Console.WriteLine("Head");
}
else if (result > 0.5 && result < 1.0)
{
Console.WriteLine("Tail");
}
//7
result = start = (random.NextDouble() * (end - start));
if (result < 0.5)
{
Console.WriteLine("Head");
}
else if (result > 0.5 && result < 1.0)
{
Console.WriteLine("Tail");
}
//7
result = start = (random.NextDouble() * (end - start));
if (result < 0.5)
{
Console.WriteLine("Head");
}
else if (result > 0.5 && result < 1.0)
{
Console.WriteLine("Tail");
}
//9
result = start = (random.NextDouble() * (end - start));
if (result < 0.5)
{
Console.WriteLine("Head");
}
else if (result > 0.5 && result < 1.0)
{
Console.WriteLine("Tail");
}
//10
result = start = (random.NextDouble() * (end - start));
if (result < 0.5)
{
Console.WriteLine("Head");
}
else if (result > 0.5 && result < 1.0)
{
Console.WriteLine("Tail");
}
//Keep console window open.
Console.ReadLine();
}
}
}

[/quote]

Hey bytetroll i took your advice here because this is what we are suppose to do for our exercise however the problem i am getting is when i create two if statements if just flips the coin once and when it lands on tails its output the number that is greater than 0.5 and says tail tail twice. when it lands on heads it outputs the number that is less than 0.5 and outputs heads heads.


import java.util.Random;
public class flip {
int head = 0;
int tail = 0;

public static void main (String[] args){
double start = 0;
double end = 1;
double random = new Random().nextDouble();
double result = start + (random * (end - start));
System.out.println(result);

if (result < 0.5){
System.out.println("head");
}else if (result > 0.5){
System.out.println("tail");
if (result < 0.5){
System.out.println("head");
}else if (result > 0.5){
System.out.println("tail");
}
}
}
}

[quote name='ByteTroll' timestamp='1349940852' post='4989003']
i am trying to make the coin flip 10 times and display how much times heads and tails come out

A loop would be an ideal way, but seeing as you can't use them, why not call 10 "if" statements in a row?

I didn't have a computer available that had Java development stuff on it, so I whipped up a small demo using C# (which is close enough to get the point). I don't know why you can't use a loop, but this is a really poor way of doing it.


using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace flip_example
{
class Program
{
static void Main(string[] args)
{
Random random = new Random();
double start = 0;
double end = 1;
double result = start = (random.NextDouble() * (end - start));
Console.WriteLine(result.ToString());
//1
if (result < 0.5)
{
Console.WriteLine("Head");
}
else if (result > 0.5 && result < 1.0)
{
Console.WriteLine("Tail");
}
//2
result = start = (random.NextDouble() * (end - start));
if (result < 0.5)
{
Console.WriteLine("Head");
}
else if (result > 0.5 && result < 1.0)
{
Console.WriteLine("Tail");
}
//3
result = start = (random.NextDouble() * (end - start));
if (result < 0.5)
{
Console.WriteLine("Head");
}
else if (result > 0.5 && result < 1.0)
{
Console.WriteLine("Tail");
}
//4
result = start = (random.NextDouble() * (end - start));
if (result < 0.5)
{
Console.WriteLine("Head");
}
else if (result > 0.5 && result < 1.0)
{
Console.WriteLine("Tail");
}
//5
result = start = (random.NextDouble() * (end - start));
if (result < 0.5)
{
Console.WriteLine("Head");
}
else if (result > 0.5 && result < 1.0)
{
Console.WriteLine("Tail");
}
//6
result = start = (random.NextDouble() * (end - start));
if (result < 0.5)
{
Console.WriteLine("Head");
}
else if (result > 0.5 && result < 1.0)
{
Console.WriteLine("Tail");
}
//7
result = start = (random.NextDouble() * (end - start));
if (result < 0.5)
{
Console.WriteLine("Head");
}
else if (result > 0.5 && result < 1.0)
{
Console.WriteLine("Tail");
}
//7
result = start = (random.NextDouble() * (end - start));
if (result < 0.5)
{
Console.WriteLine("Head");
}
else if (result > 0.5 && result < 1.0)
{
Console.WriteLine("Tail");
}
//9
result = start = (random.NextDouble() * (end - start));
if (result < 0.5)
{
Console.WriteLine("Head");
}
else if (result > 0.5 && result < 1.0)
{
Console.WriteLine("Tail");
}
//10
result = start = (random.NextDouble() * (end - start));
if (result < 0.5)
{
Console.WriteLine("Head");
}
else if (result > 0.5 && result < 1.0)
{
Console.WriteLine("Tail");
}
//Keep console window open.
Console.ReadLine();
}
}
}

[/quote]

Hey bytetroll i took your advice here because this is what we are suppose to do for our exercise however the problem i am getting is when i create two if statements if just flips the coin once and when it lands on tails its output the number that is greater than 0.5 and says tail tail twice. when it lands on heads it outputs the number that is less than 0.5 and outputs head once.


import java.util.Random;
public class flip {
int head = 0;
int tail = 0;

public static void main (String[] args){
double start = 0;
double end = 1;
double random = new Random().nextDouble();
double result = start + (random * (end - start));
System.out.println(result);

if (result < 0.5){
System.out.println("head");
}else if (result > 0.5){
System.out.println("tail");
if (result < 0.5){
System.out.println("head");
}else if (result > 0.5){
System.out.println("tail");
}
}
}
}

[/quote]
The reason you are getting the same values over and over again is beacuse you are using "result" to compare, but you never change it's value. The value of "result" is initially set using the
double result = start + (random * (end - start));

but that is never going to get executed again (thus it is going to pick the value once and keep that value). You will need to call
result = start + (random * (end - start));
after every "if...elseif" statement.

See the example I posted. After every block I recall
result = start + (random * (end - start));
giving random a new value.


EDIT: I misread your reply, so above is only an idea of why I think you are getting the issue.
EDIT2: Opps, I meant giving "result" a new value. Not "random."

"The code you write when you learn a new language is shit.
You either already know that and you are wise, or you don’t realize it for many years and you are an idiot. Either way, your learning code is objectively shit." - L. Spiro

"This is called programming. The art of typing shit into an editor/IDE is not programming, it's basically data entry. The part that makes a programmer a programmer is their problem solving skills." - Serapth

"The 'friend' relationship in c++ is the tightest coupling you can give two objects. Friends can reach out and touch your privates." - frob

ByteTroll is right. Also, the reason why you get "tail" twice and "head" only once is that you have your brackets wrong. The second set of comparisons will be executed only if the first one rolls a tail. (Look at the indentation!)

By the way, the nextDouble() function returns 0 <= x < 1. That means that 0.5 should be a tail. You don't handle it in your if, so it won't print anything if exactly 0.5 is rolled (the probability of it, as we know, is 1 to DOUBLE_VALUES_BETWEEN_0_AND_1, so it probably won't happen in testing, but it is a logic error and you could lose points in the test or introduce nasty bugs if you work with production code!). You could modify your else if to "result > 0.5" or (since you are guaranteed that result won't be bigger than 1) just modify it to "else".

Or, you know, you could just use something like

void printRoll() {
if ((new Random().nextInt(2))==0)
System.out.println("HEADS");
else
System.out.println("TAILS");
}
if (result < 0.5){
System.out.println("head");
}else if (result > 0.5){
System.out.println("tail");
}else{
System.out.println("edge");
}


;)
guys i serious dont know what i am doing. wish we should use a Loop for this exercise. Sorry i know am such a noob at this. So far this is what i got
import java.util.Random;
public class flip {
int head = 0;
int tail = 0;
int result = 0;
public static void main (String[] args){
double start = 0;
double end = 1;
double random = new Random().nextDouble();
double result = start + (random * (end - start));
System.out.println(result);

if (result < 0.5){
System.out.println("head");
}else{
System.out.println("tail");

result = start + (random * (end - start));
if (result < 0.5){
System.out.println("head");
}else{
System.out.println("tail");

result = start + (random * (end - start));
if (result < 0.5){
System.out.println("head");
}else{
System.out.println("tail");

result = start + (random * (end - start));
if (result < 0.5){
System.out.println("head");
}else{
System.out.println("tail");

result = start + (random * (end - start));
if (result < 0.5){
System.out.println("head");
}else{
System.out.println("tail");

result = start + (random * (end - start));
if (result < 0.5){
System.out.println("head");
}else{
System.out.println("tail");


result = start + (random * (end - start));
if (result < 0.5){
System.out.println("head");
}else{
System.out.println("tail");

result = start + (random * (end - start));
if (result < 0.5){
System.out.println("head");
}else{
System.out.println("tail");


result = start + (random * (end - start));
if (result < 0.5){
System.out.println("head");
}else{
System.out.println("tail");

result = start + (random * (end - start));
if (result < 0.5){
System.out.println("head");
}else{
System.out.println("tail");
}

}

}

}
}
}
}
}
}
}

}


so now the code is print out heads once and tail 10x

thanks for the help ifthen i just don't see what you are talking about. the out put looks like this

if it lands on heads
0.38712937129983
head

if it lands on tails
0.78982312093821
tail
tail
tail
tail
tail
tail
tail
tail
tail
tail

This topic is closed to new replies.

Advertisement