• Create Account

## Virtual Coin Flip

Old topic!

Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

28 replies to this topic

### #1Ryan Schurton  Members

Posted 10 October 2012 - 10:49 PM

hey i am trying to make a virtual coin flip in java.
import java.util.Random;
public class flip {
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){
}else if (result > 0.5 && result < 1.0){
System.out.println("tail");
}
}
}


currently i am trying to make the coin flip 10 times and display how much times heads and tails come out. However i cant use any loops so i know i have to call my if method 10x. i just don't know how to lay that out properly and make all the data from the 10 method calls print out. I know i got to store it in my filed variables and then have a system.out.println print out the number in heads and tails.

### #2ByteTroll  Members

Posted 11 October 2012 - 01:34 AM

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)
{
}
else if (result > 0.5 && result < 1.0)
{
Console.WriteLine("Tail");
}
//2
result = start = (random.NextDouble() * (end - start));
if (result < 0.5)
{
}
else if (result > 0.5 && result < 1.0)
{
Console.WriteLine("Tail");
}
//3
result = start = (random.NextDouble() * (end - start));
if (result < 0.5)
{
}
else if (result > 0.5 && result < 1.0)
{
Console.WriteLine("Tail");
}
//4
result = start = (random.NextDouble() * (end - start));
if (result < 0.5)
{
}
else if (result > 0.5 && result < 1.0)
{
Console.WriteLine("Tail");
}
//5
result = start = (random.NextDouble() * (end - start));
if (result < 0.5)
{
}
else if (result > 0.5 && result < 1.0)
{
Console.WriteLine("Tail");
}
//6
result = start = (random.NextDouble() * (end - start));
if (result < 0.5)
{
}
else if (result > 0.5 && result < 1.0)
{
Console.WriteLine("Tail");
}
//7
result = start = (random.NextDouble() * (end - start));
if (result < 0.5)
{
}
else if (result > 0.5 && result < 1.0)
{
Console.WriteLine("Tail");
}
//7
result = start = (random.NextDouble() * (end - start));
if (result < 0.5)
{
}
else if (result > 0.5 && result < 1.0)
{
Console.WriteLine("Tail");
}
//9
result = start = (random.NextDouble() * (end - start));
if (result < 0.5)
{
}
else if (result > 0.5 && result < 1.0)
{
Console.WriteLine("Tail");
}
//10
result = start = (random.NextDouble() * (end - start));
if (result < 0.5)
{
}
else if (result > 0.5 && result < 1.0)
{
Console.WriteLine("Tail");
}
//Keep console window open.
}
}
}


"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

### #3Álvaro  Members

Posted 11 October 2012 - 01:54 AM

POPULAR

You can also wrap every thing you are doing into a function and call it multiple times:
import java.util.Random;

public class flip {
static Random random_generator;

static void flip_once() {
double random_number = random_generator.nextDouble();

System.out.println(random_number);

if (random_number < 0.5)
else
System.out.println("tail");
}

public static void main (String[] args) {
random_generator = new Random();
flip_once();
flip_once();
flip_once();
flip_once();
flip_once();
flip_once();
flip_once();
flip_once();
flip_once();
flip_once();
}
}


### #4ByteTroll  Members

Posted 11 October 2012 - 01:59 AM

You can also wrap every thing you are doing into a function and call it multiple times:

import java.util.Random;

public class flip {
static Random random_generator;

static void flip_once() {
double random_number = random_generator.nextDouble();

System.out.println(random_number);

if (random_number < 0.5)
else
System.out.println("tail");
}

public static void main (String[] args) {
random_generator = new Random();
flip_once();
flip_once();
flip_once();
flip_once();
flip_once();
flip_once();
flip_once();
flip_once();
flip_once();
flip_once();
}
}


This is a much better way of doing it. I will second this.

"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

### #5DARSC0D3  Members

Posted 11 October 2012 - 03:18 AM

POPULAR

Another solution might be using a recursive solution.

[source lang="java"]import java.util.Random;public class flip { static Random random_generator; static void flip_coin(int repeat) { if ( repeat > 0) { double random_number = random_generator.nextDouble(); System.out.println(random_number); if (random_number < 0.5) System.out.println("head"); else System.out.println("tail"); if(--repeat > 0) flip_coin(repeat); } } public static void main (String[] args) { random_generator = new Random(); flip_coin(10); }}[/source]

### #6Álvaro  Members

Posted 11 October 2012 - 03:22 AM

Of course! I think it was Disjkstra who said loops weren't technically required in a programming language because they could always be rewritten as recursive functions.

### #7Álvaro  Members

Posted 11 October 2012 - 03:29 AM

This version is slightly more elegant:
import java.util.Random;

public class flip {
static Random random_generator;

static void flip_coin(int repeat) {
if ( repeat > 0) {
double random_number = random_generator.nextDouble();
System.out.println(random_number);

if (random_number < 0.5)
else
System.out.println("tail");

flip_coin(repeat-1); // <-- This is the only change
}
}

public static void main (String[] args) {
random_generator = new Random();
flip_coin(10);
}
}

Edited by alvaro, 11 October 2012 - 03:30 AM.

### #8DARSC0D3  Members

Posted 11 October 2012 - 04:33 AM

This version is slightly more elegant:

import java.util.Random;

public class flip {
static Random random_generator;

static void flip_coin(int repeat) {
if ( repeat > 0) {
double random_number = random_generator.nextDouble();
System.out.println(random_number);

if (random_number < 0.5)
else
System.out.println("tail");

flip_coin(repeat-1); // <-- This is the only change
}
}

public static void main (String[] args) {
random_generator = new Random();
flip_coin(10);
}
}

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.
As why there is a guard condition on top this is only because if you would be silly enough to provide any value less than 1 it would just skip the function.

Less LOC's (Lines Of Code) doesn't mean it is more elegant just remember that.

Edited by DARSC0D3, 11 October 2012 - 04:33 AM.

### #9rip-off  Moderators

Posted 11 October 2012 - 04:55 AM

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.

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:
if(repeat > 1) {
flip_coin(repeat - 1);
}

Avoiding the complex mutation expression in the condition makes the code much simpler, IMO.

### #10DARSC0D3  Members

Posted 11 October 2012 - 05:19 AM

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

I was merely explaining as why I choose to do it this way and of course in a small program you probably won't have to think about such things.

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:
if(repeat > 1) {
flip_coin(repeat - 1);
}
Avoiding the complex mutation expression in the condition makes the code much simpler, IMO.

I agree with you on that it would have been better to write it out like this, but on recalling the function i still have to disagree.
In this example there is little cost of recalling the function, but just out of habit not having to call a function that could be costly
I would prefer to not call a function if not needed.

### #11Bregma  Members

Posted 11 October 2012 - 05:23 AM

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

### #12rip-off  Moderators

Posted 11 October 2012 - 05:53 AM

Java doesn't have value objects, so creating an array with 10 elements does not create 10 instances of the class.

### #13Ryan Schurton  Members

Posted 11 October 2012 - 09:19 AM

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.

### #14Álvaro  Members

Posted 11 October 2012 - 09:36 AM

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.

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:
[...]

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.

### #15Ryan Schurton  Members

Posted 11 October 2012 - 10:05 AM

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)
{
}
else if (result > 0.5 && result < 1.0)
{
Console.WriteLine("Tail");
}
//2
result = start = (random.NextDouble() * (end - start));
if (result < 0.5)
{
}
else if (result > 0.5 && result < 1.0)
{
Console.WriteLine("Tail");
}
//3
result = start = (random.NextDouble() * (end - start));
if (result < 0.5)
{
}
else if (result > 0.5 && result < 1.0)
{
Console.WriteLine("Tail");
}
//4
result = start = (random.NextDouble() * (end - start));
if (result < 0.5)
{
}
else if (result > 0.5 && result < 1.0)
{
Console.WriteLine("Tail");
}
//5
result = start = (random.NextDouble() * (end - start));
if (result < 0.5)
{
}
else if (result > 0.5 && result < 1.0)
{
Console.WriteLine("Tail");
}
//6
result = start = (random.NextDouble() * (end - start));
if (result < 0.5)
{
}
else if (result > 0.5 && result < 1.0)
{
Console.WriteLine("Tail");
}
//7
result = start = (random.NextDouble() * (end - start));
if (result < 0.5)
{
}
else if (result > 0.5 && result < 1.0)
{
Console.WriteLine("Tail");
}
//7
result = start = (random.NextDouble() * (end - start));
if (result < 0.5)
{
}
else if (result > 0.5 && result < 1.0)
{
Console.WriteLine("Tail");
}
//9
result = start = (random.NextDouble() * (end - start));
if (result < 0.5)
{
}
else if (result > 0.5 && result < 1.0)
{
Console.WriteLine("Tail");
}
//10
result = start = (random.NextDouble() * (end - start));
if (result < 0.5)
{
}
else if (result > 0.5 && result < 1.0)
{
Console.WriteLine("Tail");
}
//Keep console window open.
}
}
}


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 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){
}else if (result > 0.5){
System.out.println("tail");
if (result < 0.5){
}else if (result > 0.5){
System.out.println("tail");
}
}
}
}


### #16Ryan Schurton  Members

Posted 11 October 2012 - 10:06 AM

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)
{
}
else if (result > 0.5 && result < 1.0)
{
Console.WriteLine("Tail");
}
//2
result = start = (random.NextDouble() * (end - start));
if (result < 0.5)
{
}
else if (result > 0.5 && result < 1.0)
{
Console.WriteLine("Tail");
}
//3
result = start = (random.NextDouble() * (end - start));
if (result < 0.5)
{
}
else if (result > 0.5 && result < 1.0)
{
Console.WriteLine("Tail");
}
//4
result = start = (random.NextDouble() * (end - start));
if (result < 0.5)
{
}
else if (result > 0.5 && result < 1.0)
{
Console.WriteLine("Tail");
}
//5
result = start = (random.NextDouble() * (end - start));
if (result < 0.5)
{
}
else if (result > 0.5 && result < 1.0)
{
Console.WriteLine("Tail");
}
//6
result = start = (random.NextDouble() * (end - start));
if (result < 0.5)
{
}
else if (result > 0.5 && result < 1.0)
{
Console.WriteLine("Tail");
}
//7
result = start = (random.NextDouble() * (end - start));
if (result < 0.5)
{
}
else if (result > 0.5 && result < 1.0)
{
Console.WriteLine("Tail");
}
//7
result = start = (random.NextDouble() * (end - start));
if (result < 0.5)
{
}
else if (result > 0.5 && result < 1.0)
{
Console.WriteLine("Tail");
}
//9
result = start = (random.NextDouble() * (end - start));
if (result < 0.5)
{
}
else if (result > 0.5 && result < 1.0)
{
Console.WriteLine("Tail");
}
//10
result = start = (random.NextDouble() * (end - start));
if (result < 0.5)
{
}
else if (result > 0.5 && result < 1.0)
{
Console.WriteLine("Tail");
}
//Keep console window open.
}
}
}


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 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){
}else if (result > 0.5){
System.out.println("tail");
if (result < 0.5){
}else if (result > 0.5){
System.out.println("tail");
}
}
}
}


### #17ByteTroll  Members

Posted 11 October 2012 - 10:26 AM

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."

Edited by ByteTroll, 11 October 2012 - 03:52 PM.

"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

### #18ifthen  Members

Posted 11 October 2012 - 10:57 AM

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)
else
System.out.println("TAILS");
}


Edited by ifthen, 11 October 2012 - 10:58 AM.

### #19Álvaro  Members

Posted 11 October 2012 - 11:15 AM

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


;)

### #20Ryan Schurton  Members

Posted 11 October 2012 - 06:24 PM

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 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){
}else{
System.out.println("tail");

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

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

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

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

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

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

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

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

result = start + (random * (end - start));
if (result < 0.5){
}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

0.38712937129983

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

Old topic!

Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.