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();
}
}