Question

In: Computer Science

Suppose you have access to a random number generator Rng() that generates uniform random numbers in...

Suppose you have access to a random number generator Rng() that generates uniform random numbers in {0, 1, . . . , n − 1}. Design a function uses Rng() generate a uniform random number in {0, 1, . . . , m − 1}, where m ≤ n

Solutions

Expert Solution

Notice that the function rng() can return numbers from anywhere between 0 to (n-1).

If we want to produce a random value between 0 to (m-1), m<=n, and easy step is to limit the range of values by taking the remainder of the number with m.

Consider this example:

Take a random number generator that can generate any number between 0 to 999 (n = 1000). If we then take the remainder of the output of this function with the value 8 (m = 8), the results we get are in the range (0%8) to (999%8). Then, the only values we can get are 0, 1, 2, 3, 4, 5, 6, and 7, regardless of what the value of "n" in the random generator is. Hence, taking the remainder of the random number generator rng() by the value m gives us the desired value.

When m == n, when we take the remainder with n, we just get the original value back.

Hence, we can define the function rng_m():

function rng_m(m): 
    return rng() % m 

(If you liked this answer, feel free to give it a thumbs up. Thank you!)


Related Solutions

A random number generator picks a number from 7 to 68 in a uniform manner. Round...
A random number generator picks a number from 7 to 68 in a uniform manner. Round answers to 4 decimal places when possible. The mean of this distribution is The standard deviation is The probability that the number will be exactly 42 is P(x = 42) = The probability that the number will be between 16 and 28 is P(16 < x < 28) = The probability that the number will be larger than 48 is P(x > 48) =...
A random number generator picks a number from 18 to 64 in a uniform manner. Round...
A random number generator picks a number from 18 to 64 in a uniform manner. Round answers to 4 decimal places when possible. The mean of the distribution is: The standard deviation is: The probability that the number will be exactly 20 is P(x = 20) = The probability that the number will be between 24 and 26 is P(24 < x < 26) = The probability that the number will be larger than 32 is P(x > 32) =...
A random number generator returns random floats between 0 and 1, according to a uniform distribution....
A random number generator returns random floats between 0 and 1, according to a uniform distribution. say you let the random number generator draw 100 numbers, the sample mean will be... A.impossible to determine, because the data aren't drawn from a normal distribution. B.normally distributed, with mean 0.5 and standard deviation 0.2887. C.uniformely distributed, between 0 and 1. D.normally distributed, with mean 0.5 and standard deviation 0.02887.
continuous random variable and uniform distribution please follow the comment. A random number generator spits out...
continuous random variable and uniform distribution please follow the comment. A random number generator spits out a random real number in the range [1,4] assume each number is equally likely being out. what is the probability that the model output an irrational number? the answer is 1, but i don't understand
Questions 36-39: Six numbers are selected at random from a random number generator and entered into...
Questions 36-39: Six numbers are selected at random from a random number generator and entered into each of four rows. The summary statistics are presented in the chart below. Mean SD   Row 1 5.8  0.78 Row 2 3.6  0.23 Row 3 5.5  0.32 Row 4 4.0  0.59 Assume the populations are normal with equal variances. It is of interest to test the following: Ho: μ1 = μ2 = μ3 = μ4 36. What are the degrees of freedom for the...
Use a random number generator to produce 1000 uniformly distributed numbers with a mean of 10, a
Use a random number generator to produce 1000 uniformly distributed numbers with a mean of 10, a minimum of 2, and a maximum of 18. Obtain the mean and the histogram of these numbers, and discuss whether they appear uniformly distributed with the desired mean.
Use a random number generator to produce 1000 normally distributed numbers with a mean of 20
Use a random number generator to produce 1000 normally distributed numbers with a mean of 20 and a variance of 4. Obtain the mean, variance, and histogram of these numbers, and discuss whether they appear normally distributed with the desired mean and variance.
Below are 50 random numbers taken from a random number generator. 0.534 0.401 0.401 0.445 0.445...
Below are 50 random numbers taken from a random number generator. 0.534 0.401 0.401 0.445 0.445 0.125 0.094 0.094 0.104 0.104 0.345 0.259 0.259 0.288 0.288 0.785 0.589 0.589 0.654 0.654 0.975 0.731 0.731 0.813 0.813 0.834 0.626 0.626 0.695 0.695 0.683 0.512 0.512 0.569 0.569 0.322 0.242 0.242 0.268 0.268 0.526 0.395 0.395 0.438 0.438 0.234 0.176 0.177 0.198 0.196 a) Using 5 class intervals, determine the computed chi-square value. Answer in 1 decimal place.    b) What is...
1. A random number generator claims to randomly choose real numbers between 0 and 3000. (a)...
1. A random number generator claims to randomly choose real numbers between 0 and 3000. (a) If this is true, what kind of distribution would the randomly chosen numbers have? (b) What would be the mean of this distribution? (c) What would be the standard deviation of this distribution? (d) Say we take a sample of 65 generated numbers and obtain a sample mean of 1552. What do we know about the sampling distribution of the sample mean and how...
1- Write a function f(n,a,b) that generates n random numbers # from Uniform(a,b) distribution and returns...
1- Write a function f(n,a,b) that generates n random numbers # from Uniform(a,b) distribution and returns their minimum. # Execute the function with n=100, a=1, b=9. 2- Replicate the function call f(100,1,9) 100 thousand times # and plot the empirical density of the minimum of 100 indep. Unif(1,9)'s 3-Use the sampling distribution from (b) to find 95% confidence # interval for the minimum of 100 independent Unif(1,9)'s. Please solve in R
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT