Question

java language question: Write a thread that tosses a coin 1000 times and computes the frequency...

java language question:

Write a thread that tosses a coin 1000 times and computes the frequency of heads and tails. A coin can be modelled by a random number generator that generates random numbers in the range 0..1.
0 0
Add a comment Improve this question Transcribed image text
Answer #1

import java.util.Random;

//class coinToss
class coinToss extends Thread
{
public void run()
{
//variable declaration and initialization
int toss, tail=0, head=0;
Random rand = new Random();
  
for(int i=0; i<1000; i++)
{
//assign random number 0 or 1
toss = rand.nextInt(2);
//we assume that if random number is 1 then it is head otherwise tail
if(toss == 1)
{
head++;
}
else
{
tail++;
}
}
//display the frequency of head
System.out.println("Frequency of heads: " + head);
//display the frequency of tail
System.out.println("Frequency of tails: " + tail);
}
}

//main class
public class Main
{
   public static void main(String[] args)
   {
       coinToss toss = new coinToss();
       //method calling
toss.start();
   }
}

OUTPUT:

Add a comment
Know the answer?
Add Answer to:
java language question: Write a thread that tosses a coin 1000 times and computes the frequency...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT