Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Problem 4 Coding Assignment: In this problem you will develop code to generate r

ID: 3745431 • Letter: P

Question

Problem 4 Coding Assignment: In this problem you will develop code to generate random numbers that follow a particular distribution. Common programming languages provide you with a (pseudo) random number generator that yields a uniformly distributed random variable. This question is about using such capability to develop a generator for arbitrarily distributed random vari- ables a) Write a Java function with the following prototype: double exp(double lambda), that returns a random value that is distributed according to an exponential distribution with a mean of T - (Hint: An approach for doing this is provided in the lecture notes). The value of A will be passed as a parameter to your code. The function should be implemented in its own java file, named exp.java Apart from implementing the function itself, the class should also include a main(...) function. The main(...) function should accept 1 parameter from the calling environment, i.e. a double holding the value of A It is responsibility of the main(...) function to internally invoke the implemented exp(...) function only once, and to print its result. Nothing else should be printed in the console, except the value returned by exp(...) in decimal format. b) Write a Java function with prototype: double custom (double [] outcomes, double [] probabilities), that returns a random value that is distributed according to an arbitrary distribution described in a table of discrete values/outcomes (for the random variable) and the probability of those values. (Hint An approach for doing this is provided in the lecture notes). The function should be implemented in its own java file, named custom.java. Apart from implementing the function itself, the class should also include a main(...) function. The main(...) function should accept 10 parameters from the calling environment. These 10 parameters represent 5 pairs of outcome values and associated probabilities. For instance, the custom distribution given in Table 1 is given in input in the following way: 5 0.16 10 0.13 20 0.22 40 0.20 70 0.29 Outcome Probability 10 20 40 70 0.16 0.13 0.22 0.20 0.29 Table 1: A custom probability distribution It is responsibility of the main(...) function to internally invoke the implemented custom(...) function only once, and to print its result. Nothing else should be printed in the console, except the value returned by custom(...) in decimal format.

Explanation / Answer

Answer a:

package ClassProg;

import java.util.*;

// Class Exp definition

public class Exp

{

// Static method to return a random number between two given number

static double exp(double lambda)

{

// Random class object created

Random rand = new Random();

// Calculates max range

double max = 1.0 / lambda;

// Generates a random double number and returns it

return (1.0 + (max - 1.0) * rand.nextDouble());

}// End of method

// main method definition

public static void main(String ss[])

{

// Displays result with 2 decimal places

System.out.printf(" Result = %.2f", exp(Double.parseDouble(ss[0])));

}// End of method main

}// End of class

Sample Output:

Result = 0.88

Answer b:

package ClassProg;

import java.util.Random;

// Defines class Custom

public class Custom

{

// Method to generate a random number and return the probability array random index position value

public static double custom(double [] outcomes, double [] probability)

{

// Random class object created

Random rand = new Random();

// Generates a random number between 0 and length of the outcomes array

// Returns the probability array random number index position value

return probability[rand.nextInt(outcomes.length) + 0];

}// End of method

// main method definition

public static void main(String ss[])

{

// Declares array of size 5

double [] outcomes = new double[5];

double [] probability = new double[5];

// Loops till length of the command line argument divided by 2 times

for(int x = 0, c = 0; x < ss.length/2; x++, c+=2)

{

// Stores the command line c index position data at x index position of outcomes array

outcomes[x] = Double.parseDouble(ss[c]);

// Stores the command line c + 1 index position data at x index position of probability array

probability[x] = Double.parseDouble(ss[c+1]);

}// End of for loop

// Displays result with 2 decimal places

System.out.printf(" Result = %.2f", custom(outcomes, probability));

}// End of method main

}// End of class

Sample Output:

Result = 0.20

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote