CS 1301 Lab 8

100 Points

Attendance is worth 10 points.

(50 points) Team Problem 1:
For this program you need to submit the source code (.java), including prolog.

Random numbers generated by computers are only as good as the algorithms used. One important property of random numbers is their distribution. Below, you will find code for a main function which you cannot change for this lab, that prompts the user for n---the number of samples to be generated by the random number generator (with integer values from 0 to 9), and stores the random numbers into an array called samples. The code then makes a call to a function called computeHistogram, that you have to write, which computes the histogram of that sample distribution.

A histogram is a count of how many times each element from a set occurs. If your sample consists of numbers between 0 and 9 inclusive, your histogram will have 10 elements (or bins), counting occurence of each element.

For example, if your sample consists of {0, 0, 1, 2, 5, 5, 5, 5, 7, 1, 1} and you know the possible values are between 0 and 9 inclusive, the histogram should be an array of length 10 with the following values:

[2, 3, 1, 0, 0, 4, 0, 1, 0, 0] 
The first element (index zero) counts how many 0's there were in the array samples, in the sample above there were 2 zero's. The second element, index 1, counts how many ones were in the array samples; in the example, there were 3 instances of 1. The third element, index 2, counts how many 2's where in the array samples (in the example above there was one 2), and so on.

Below are some sample runs for various options of n:

Sample run 1:
Please enter n (number of random numbers): 100
The histogram of the sample distribution is: 
0: 10 1: 8 2: 8 3: 6 4: 8 5: 19 6: 12 7: 9 8: 11 9: 9 

Sample run 2:
Please enter n (number of random numbers): 1000
The histogram of the sample distribution is: 
0: 112 1: 77 2: 87 3: 107 4: 117 5: 97 6: 98 7: 103 8: 105 9: 97 

Sample run 3:
Please enter n (number of random numbers): 100000
The histogram of the sample distribution is: 
0: 9968 1: 10026 2: 10219 3: 9966 4: 9863 5: 10021 6: 9991 7: 9830 8: 9982 9: 10134 

Sample run 4:
Please enter n (number of random numbers): 10000000
The histogram of the sample distribution is: 
0: 998783 1: 998017 2: 1001165 3: 1001234 4: 1000032 5: 1001210 6: 999909 7: 999723 8: 1000085 9: 999842 


Here is the code for the main function:

import java.util.Scanner;
import java.util.Random;


public class lab8 {

   
// WRITE YOUR FUNCTION computeHistogram here 
// the function should return an array of integers 
// and have one parameter, an array of integers with 
// possible values between 0 and 9 (9 included)








// END OF FUNCTION

  
public static void main(String[] args)
{
	Scanner s = new Scanner(System.in);
	System.out.print("Please enter n (number of random numbers): ");
	int n = s.nextInt();
      
	Random r = new Random();
      
	int[] samples = new int[n];
	for(int ix = 0; ix < samples.length; ix++) 
		samples[ix] = r.nextInt(10); // this will be in the range [0, 9]
      
	int[] histogram = computeHistogram(samples);
      
	System.out.println("The histogram of the sample distribution is: ");
	for(int ix = 0; ix < histogram.length; ix++)
		System.out.print(ix + ": " + histogram[ix] + " ");
}
	
}

(40 points) Individual Problem 1:
Write two paragraphs about the distribution of the random number generator used in this lab. Does it produce a uniform distribution? If so, how did you reach that conclusion? Justify based on runs of the program from problem 1.

To successfully complete this lab you should: