Complete the following definition for the function coinFlip which uses a random
ID: 3690976 • Letter: C
Question
Complete the following definition for the function coinFlip which uses a random number generator to simulate the toss of a biased coin. The function accepts the probability of ’obtaining a heads’ which is of type double and returns either HEADS, or TAILS. Note: nextDouble() is a function which returns the next pseudorandom, uniformly distributed double value between 0.0 and 1.0 from this random number generator’s sequence. public enum Face { _____ , _____ } public _____ flipCoin ( _____ ) { Random rand = new Random (); if ( rand . nextDouble () <= _____ ) return _____ ; // Return heads else return _____ ; // Return tails }
Explanation / Answer
public enum Face { heads , tails }
public Face flipCoin (double p )
{
Random rand = new Random ();
if ( rand . nextDouble () <= p )
return heads ; // Return heads
else return tails ; // Return tails
}