简体   繁体   中英

Cannot get Random number

I have been working to get the headcount variable to be random but cannot figure this out

public class Coin
{
    private final int HEADS = 0;
    private final int TAILS = 1;
    private int face;
    private static int seed =0;
    private Random r;
    public Coin ()
    {
        r = new Random(seed);
        flip();
        seed++;
    }
    public void flip ()
    {
        face = r.nextInt(2);
    }
    public int getFace()
    {
        return face;
    }
    public void setFace(int newFace)
    {
        face = newFace;
    }
    public boolean isHeads ()
    {
        return (face == HEADS);
    }
    public String toString()
    {
        String faceName;
        if (face == HEADS)
            faceName = "Heads";
        else
            faceName = "Tails";
        return faceName;
    }
    public static void main(String[] args)
    {
        Coin myCoin = new Coin();
       double randnumber =Math.random();
        int headCount=0;
        for (int i =1; i<=100; i++)
        {
            myCoin.flip();
            if(myCoin.isHeads())
            {
                headCount++;
            }
        }
        System.out.println("If I flip my coin 100 times, I get " + headCount + " heads.");
        headCount =0;
        for (int i =1; i<=100; i++)
        {
            Coin yourCoin = new Coin();
            yourCoin.flip();
            if(yourCoin.isHeads())
            {
                headCount++;
            }
        }
        System.out.println("If I flip 100 coins, I get " + headCount + " tails.");

    }
}

Whenever I Compile it and Run the program I get the same output which is

If I flip my coin 100 times, I get 47 heads. If I flip 100 coins, I get 50 tails.

I dont understand how to make the 47 and 50 to be new random numbers each time you run the program. I have looked int Math.Random and other random variables but am unsure how to implement it into this program.

What you want is: r = new Random(System.currentTimeMillis());

BTW: Random Numbers generators always start with the same number, given a certain seed.

For example, supposed the "random number" generator added 2 to find the next random number.

seed = 9 r(9) = 11 ; r(11) = 13 ; r(13) = 15.

However, a different seed will lead to a different string of numbers.

seed = 4 r(4) = 6 ;

Random numbers in computer programming aren't really random. They depend on a seed value. If you give it the same seed each time, you'll get the same sequence of "random" values each time.

If, instead, you use the empty constructor for Random, it will create a seed for you that is based on the current system nano time. In this way, you will mitigate the chance that you start with the same seed.

So, as others have already mentioned, either call the empty Random constructor, or create a pseudo-random seed yourself before creating a Random that takes the seed argument.

With all that said, it's probably best to assume the JDK implementers have already thought about how best to generate a seed for you, so you should just use the empty constructor.

If you want to see the Java source for the Random class, you can search for

grepcode java.util.Random.java 

and see how the JDK implementers create the seed.

Solve the seed issue by declaring Random in the class directly instead in a method.

private Random r = new Random();

public Coin()
{
    int x = r.nextInt();
    flip();
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM