Results 1 to 3 of 3
  1. #1
    Join Date
    Nov 2015
    Posts
    1
    Rep Power
    0

    Default java.util.Random problem

    I wrote a program that essentially flips a coin until heads is flipped consecutively the inputted amount of times. The program worked sometimes, and sometimes is didn't, so I wrote a statement that outputs the number of flips every 10000 flips. When the program runs as intended, the final output is usually around 15000, but sometimes it rockets off into the millions and I have to manually end the program. Why is this? The program is below(sorry for the useless variables).

    import java.util.Random;
    import java.util.Scanner;
    public class CoinFlipper{
    public static void main(String[] args){

    Scanner in=new Scanner(System.in);
    System.out.println("Enter a number:");
    int b=in.nextInt();


    long z=0;
    int c=0;
    int d=0;

    while(c<b)
    {
    {
    Random generator=new Random();
    int a=generator.nextInt(50)+1;
    if(a<=25)
    {
    d++;
    c++;
    }

    if(a>25)
    {
    int f=b-5;
    int e=d;
    c=0;
    d=0;
    }
    z++;
    }
    if(z%10000==0)
    {
    System.out.println(z);
    }
    }

    System.out.println("The program has flipped heads "+b+" times in a row after flipping the coin "+z+" times");

    }
    }

  2. #2
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    6,226
    Rep Power
    14

    Default Re: java.util.Random problem

    I believe that 15000 is too few (although statistically you could do it in the first 25 flips). The odds are 2^n where n is the number of heads or tails in a row. You also, only need to create one instance of Random (before you enter the loop).

    Regards,
    Jim
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

  3. #3
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    20,003
    Rep Power
    33

    Default Re: java.util.Random problem

    final output is usually around 15000
    Wouldn't the number of flips depend on the number in a row requested?

    sorry for the useless variables
    Yes, the names of the variables are useless. Poorly named variables make a program much harder to understand both for the reader and the writer of the code.
    If you don't understand my response, don't ignore it, ask a question.

Similar Threads

  1. java.util.Scanner problem
    By CecoI in forum New To Java
    Replies: 13
    Last Post: 06-11-2014, 03:48 PM
  2. Replies: 12
    Last Post: 09-15-2013, 09:09 PM
  3. Replies: 0
    Last Post: 01-23-2012, 09:12 AM
  4. util.random help.
    By fishy8158 in forum New To Java
    Replies: 3
    Last Post: 11-18-2011, 10:04 PM
  5. Replies: 3
    Last Post: 10-24-2010, 10:24 PM

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •