Question: JAVA package tests; import java.util.Random; import java.util.Scanner; public class RandomPicker { public static void main(String[] args) { // get prompt from user System. out .println(Enter
JAVA
package tests; import java.util.Random; import java.util.Scanner; public class RandomPicker { public static void main(String[] args) { // get prompt from user System.out.println("Enter a number: "); int n= new Scanner(System.in).nextInt(); // random number generator Random random = new Random(); int pick1,pick2,pick3; // get pick1 pick1 = random.nextInt(n); // loop till all the picks are equal do { pick2= random.nextInt(n); pick3= random.nextInt(n); }while ((pick1!=pick2) || (pick1!=pick3)); System.out.printf("The number picked are: %d %d %d ", pick1, pick2, pick3); } }
Examine your three-of-a-kind code from the previous question. In the text box below determine the time complexity of the code:
2. Randomly picks an integer between 1 and n. Save this in a variable pick1 3. Randomly picks an integer between 1 and n. Save this in a variable pick2 4. Randomly picks an integer between 1 and n. Save this in a variable pick3 5. Go to step 3 until pick1 is equal to pick2 AND pick1 is equal to pick3
Hint: To answer this question you have to think about "probabilities". What is the probability that a specific number between 1 and n is chosen at random? Run your program multiple times using n=5 and extrapolate a general f(n) from this example (more hint: if n=5 then the odds of picking some number is one in five so what are the odds of picking the same number twice?).
Answer the following questions:
1. Determine a worst-case time complexity formula f(n) for the algorithm (highlighted in red above).
2. Determine worst case run-time Big-O i.e: O(f(n))
3. Determine best case run-time. i.e: Omega f(n)
4. Estimate Theta f(n) . ( average run-time ).
Note: fyou must THOROUGHLY explain your reasoning for your answers for each question 1-4.
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
