Virtual Coin Flip

Started by
27 comments, last by StayFly 11 years, 6 months ago
hey i am trying to make a virtual coin flip in java.
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 && 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.

any help please? thanks you
Advertisement
i am trying to make the coin flip 10 times and display how much times heads and tails come out[/quote]
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();
}
}
}

"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

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

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

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

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

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

if(repeat > 1) {
flip_coin(repeat - 1);
}

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

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.

This topic is closed to new replies.

Advertisement