Random number generators initally provide uniformly distribu

Random number generators initally provide uniformly distributed random numbers in a interval of unit length, but often we need to sample from other distributions. Suppose that, we wish to simulate a random variable n drawn from F_binom (n, N = 3, p). We can do this by partitioning the unit segment into four bins of width (1 - p)^3, 3p(1 - p)^2, 3p^2(1 - p) and p^3, corresponding to n = 0, 1, 2, 3. The width of each bin is thus proportional to the probability of occurrence. To sample from Pbm0m(n, N, p) one thus simply draws a uniformly distributed random number and finds the bin index into which that number falls. (a) Write a function binom_setup that accepts a value of p and returns a list of locations of the bin edges appropriate for N=5. (b) Write another function binom_dist that accepts the list of bin edges as input and returns a single binomially distributed random variable.

Solution

package com.mkyong.example.test; import java.util.Random; public class TestRandom { public static void main(String[] args) { for (int i = 0; i < 10; i++) { System.out.println(getRandomNumberInRange(5, 10)); } } private static int getRandomNumberInRange(int min, int max) { if (min >= max) { throw new IllegalArgumentException(\"max must be greater than min\"); } Random r = new Random(); return r.nextInt((max - min) + 1) + min; } }
 Random number generators initally provide uniformly distributed random numbers in a interval of unit length, but often we need to sample from other distributio

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site