Question

In: Computer Science

Write a program that takes an integer N from the command line and uses StdRandom.uniform() to...

Write a program that takes an integer N from the command line and uses StdRandom.uniform() to generate a random sequence of integers be- tween 0 and N – 1. Run experiments to validate the hypothesis that the number of integers generated before the first repeated value is found is ~√?N/2.

Solutions

Expert Solution

// Please refer to the screenshot of the code to understand indentation.

// Feel free to ask any doubts by commenting on the answer

// You will need 2 additional files because of the requirement to use StdRandom.uniform() function (StdRandom.java and StdOut.java). Download those files from Princeton University's database and keep them in the same folder in which you keep the following code.

// I've generated a sequence till an integer is repeated. This generation was repeated for 1000 times so that an average result value could be obtained to compare it to the hypothesised value.

public class Main
{
   public static void main(String[] args) {
   // N = Value of N from command line
   int N =   Integer.parseInt(args[0]);
   // numRepeats = Number of times sequence is generated
   int numRepeats = 1000;
   // numInts = Average number of integers to be generated before the first repeated value is found
   int numInts = 0;
   // hypothesis = Hypothesised number of integers root of (2*pi/n)
   double hypothesis = Math.sqrt(Math.PI * N / 2);
   // Generate a sequence until a repeated value is found for numRepeats(=1000) times
   for (int i=0; i<numRepeats; i++)
   {
   // Value at index i of arr is 1 if the integer i has been generated previously
   int[] arr = new int[N];
   // x = Integer generated by function uniform
   int x = StdRandom.uniform(N);
   // counter = Number of integers generated before a repeated integer is found in the current sequence
   int counter = 1;
   // Repeatedly generate integers until the first repeated integer is found
   while (arr[x] != 1)
   {
   // Flick the value at x index of arr to 1
   arr[x] = 1;
   // Generate a new integer and store it in x
   x = StdRandom.uniform(N);
   // Increase the variable counter by 1
   counter++;
   }
   // Accumulate counter to numInts
   numInts += counter;
   }
   // Divide numInts by numRepeats to obtain the average numInts
   numInts = numInts/numRepeats;
  
   // Print the final information
   System.out.println("N: " + N);
   System.out.println("On repeating the sequence 1000 times, a repeated value was found after "
   + numInts + " integers on average.");
   System.out.println("Hypothesised number of integers: " + hypothesis);
   }
}


Related Solutions

Write a C program called test that takes one command line argument, an integer N. When...
Write a C program called test that takes one command line argument, an integer N. When we run test: ./test N the program will do this: the parent process forks N child processes each child process prints its process ID, exits the parent process waits for all child processes to exit, then exits
Write a program in C or in Java, that takes an integer value N from the...
Write a program in C or in Java, that takes an integer value N from the command line, generates N random points in the unit square, and computes the distance separating the closest pair of points. A unit square is a square with sides of length 1, at points (0, 0), (0, 1), (1, 0), and (1, 1). If you wish to avoid the command-line processing, you can just assume you will generate a fixed number of points, say between...
Write a Java program which reads a positive integer from the command line, then displays the...
Write a Java program which reads a positive integer from the command line, then displays the sum of all even values up to and including the value provided, followed by the sum of all odd values up to and including the value provided. validate that the command line argument is an integer greater than 0 to validate the type, you can use Integer.parseInt() with a try/catch for NumberFormatException use one or more for loops to perform the even and odd...
Write a program that takes two integer arrays a and b of size n from the...
Write a program that takes two integer arrays a and b of size n from the user, the use a method product to find the product of a and b and return the results after storing them in an array c, then prints the returned results to the screen. (Note: c[i] = a[i] * b[i], for i = 0, ..., n-1) Sample Output: Enter the size of your arrays: 5 Enter the integer values of the first array a: 1...
Write a program that takes two command line arguments at the time the program is executed....
Write a program that takes two command line arguments at the time the program is executed. You may assume the user enters only decimal numeric characters. The input must be fully qualified, and the user should be notified of any value out of range for a 23-bit unsigned integer. The first argument is to be considered a data field. This data field is to be is operated upon by a mask defined by the second argument. The program should display...
write a program ordered.java Order check Write a program Ordered.java that takes four int command-line arguments...
write a program ordered.java Order check Write a program Ordered.java that takes four int command-line arguments w, x, y, and z. Define a boolean variable whose value is true if the four values are either in strictly ascending order (w < x < y < z) or strictly descending order (w > x > y > z), and false otherwise. Then, display the boolean variable value. NOTE 1: Do not use if statements on this program. NOTE 2: Assume that...
Python Write a program that takes a text filename as command line argument, and prints number...
Python Write a program that takes a text filename as command line argument, and prints number of times each letter occurred in this file.
Write a program in Objective C that takes an integer keyed in from the terminal and...
Write a program in Objective C that takes an integer keyed in from the terminal and extracts and displays each digit of the integer in Eglish. So if the user types 647, the program should display the following: six four seven
Write a C or C++ program A6p2.c(pp) that accepts one command line argument which is an integer n between 2 and 4 inclusi...
Write a C or C++ program A6p2.c(pp) that accepts one command line argument which is an integer n between 2 and 4 inclusive. Generate 60 random integers between 1 and 49 inclusive and store them in a 5 by 12 two dimensional integer array (e.g.,int a[5][12];). Use pthread to create n threads to square all 60 array elements. You should divide this update task among the n threads as evenly as possible. Print the array both before and after the...
In Python, write a program to get a positive integer n from keyboard first (your program...
In Python, write a program to get a positive integer n from keyboard first (your program must be able to check the validation of n being positive), and then get n integers from keyboard and store these n integers in a list. Do some processing, and print out average of the elements in the list at end of your program. For example, suppose user enters 3 first (n = 3), which means we get another three integers from the user....
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT