0

I apologize in advance for my lack of Java knowledge. I am new to Java programming and am trying to make a program where I can flip a coin and count how many times the coin lands on heads within N amount of rolls, measure the time it takes to do so, and then print it out in the Console so that I can save it in a .txt file. I think I've almost gotten it; I'm just having difficulties printing it out now. Any help would be appreciated! I'm stuck!

import java.util.Random;
import java.io.*;

public class RollGraph
{
    public static void flip(int n)
    {
        Random rnd = new Random();
        int roll = 0;
        int countHeads = 0;
        int headsInRow = 0;
        int headsOrTails = rnd.nextInt(2);

        while(roll<n){

            if(headsOrTails == 1){
                countHeads++;
                headsInRow++;
            }
            else{
                headsInRow=0;
            }
        }
        return;
    }

    public static void main(String[] arg) throws IOException
    {
        BufferedWriter writer = new BufferedWriter(
                new FileWriter( new File("data.txt")));
        long start,end,elapsed;
        int repeat = 20;
        double total;
        double average;

        for(int n=1;n<100;n++)
        {
            total = 0.0;
            for(int j=0;j<repeat;j++)
            {
                start = System.nanoTime();
                flip(n);
                end = System.nanoTime();
                elapsed = end - start;
                total += elapsed/1000000;
            }
            average = total/repeat;
            String line = n+"\t"+ average+"\t"+Math.log(average);
            System.out.println(line);
            writer.write(line);
            writer.newLine();
            writer.flush();
        }
        writer.close();
    }
}
Sr.Richie
  • 5,680
  • 5
  • 38
  • 62
Bfrank
  • 33
  • 1
  • 4
  • 12

3 Answers3

0

In the flip method in this loop while(roll<n){,
here you don't increment the roll variable.

This is one problem I see.

Check the logic of your flip method. Does not seem right to me.

peter.petrov
  • 38,363
  • 16
  • 94
  • 159
0

I see two problems. First, you have an infinite loop in flip, because you don't increment roll at all. Increment it.

Second, you have integer division on this line:

total += elapsed/1000000;

In Java, dividing two ints must result in an int, so you will probably get a bunch of zeroes here. Use a double literal or cast elapsed to double to perform floating-point arithmetic.

total += elapsed/1000000.0;

OR

total += (double) elapsed/1000000;
rgettman
  • 176,041
  • 30
  • 275
  • 357
0

In addition to the infinite loop and division problems already mentioned, if you want to make your print lines cleaner, I recommend using BigDecimal to round your doubles:

String line = n+"\t"+ BigDecimal.valueOf(average).setScale(5, BigDecimal.ROUND_HALF_UP) + "\t"+ BigDecimal.valueOf(Math.log(average)).setScale(5, BigDecimal.ROUND_HALF_UP);
jlewkovich
  • 2,725
  • 2
  • 35
  • 49