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 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 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...
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...
Write a program that takes in a positive integer as input, and outputs a string of...
Write a program that takes in a positive integer as input, and outputs a string of 1's and 0's representing the integer in binary. For an integer x, the algorithm is: As long as x is greater than 0 Output x % 2 (remainder is either 0 or 1) x = x / 2 Note: The above algorithm outputs the 0's and 1's in reverse order. Ex: If the input is: 6 the output is: 011 6 in binary is...
IN PYTHON Write a program that takes in a positive integer as input, and outputs a...
IN PYTHON Write a program that takes in a positive integer as input, and outputs a string of 1's and 0's representing the integer in binary. For an integer x, the algorithm is: As long as x is greater than 0 Output x % 2 (remainder is either 0 or 1) x = x // 2 Note: The above algorithm outputs the 0's and 1's in reverse order. You will need to write a second function to reverse the string....
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....
Introduction Write in C++ at the Linux command line a program that is the same as...
Introduction Write in C++ at the Linux command line a program that is the same as the previous collection app project but now uses a class to store the items and also can save the items to a file that can be read back into the array by the user when the program is re-started. You can use your project 1 submission as a starting point or you can do something new as long as it meets the listed requirements....
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT