Question

In: Statistics and Probability

The first random number generator comes from Python itself. To use it you need to import...

The first random number generator comes from Python itself.

To use it you need to import the Python package. Then call the random() method. See below.

import random

print (random.random())

It is important to know that a random number generator really is random. If so, it should have uniform distribution between 0 and 1. Test the random number generators in Python for uniformity.

Solutions

Expert Solution

What you are asking for is not random; indeed, it is fairly structured. The point of random data is that it isn't structured: you can't say anything about what the values will be, only what they will be on average.

You can still do what you want, though: just specify the ranges you want your numbers to fall into.

>>> def random_function(*args):
...     n = sum(args)
...     bottom = 0
...     for m in args:
...             for _ in range(m):
...                     yield random.uniform(bottom, bottom + m/n)
...             bottom += m/n
...
>>> list(random_function(6,3,1))
[0.1849778317803791, 0.2779140519434712, 0.08380168147928498, 0.5477412922676888
, 0.5158697440011519, 0.5535466918038039, 0.8046993690361345, 0.714614514522802,
 0.7102988180048052, 0.9608096335125095]
>>> list(random_function(6,3,1))
[0.29313403848522546, 0.5543469551407342, 0.14208842652528347, 0.344464024804118
7, 0.3168266508239002, 0.5956620829410604, 0.673021479097414, 0.7141779120687652
, 0.7783099010964684, 0.9103924993423906]

Explanation:

Given (6,3,1), work out their sum (10). We want six numbers between 0 and 6/10, three numbers between 6/10 and 6/10 + 3/10, and one number between 6/10 + 3/10 and 6/10 + 3/10 + 1/10. We can generate these by calling

random.uniform(0, 6/10) six times,
random.uniform(6/10, 9/10) three times, and
random.uniform(9/10, 1) once.

Another approach could be

As part of the random module (import random), you can use random.uniform(a,b) instead of random.random() where a to b is the range in floating point.

But second approach may or may not have significant effect on uniformity. So, its better to go with first approach.


Related Solutions

Use Python. Create a Shakespearean Insult Generator The generator takes one element / data chunk from...
Use Python. Create a Shakespearean Insult Generator The generator takes one element / data chunk from each list and combines them to form the Shakespearean Insult. For example - List1 - artless List2 - beef-witted List3 - barnacle Result - Thou is a artless beef-witted barnacle    Your program will have three modes. There is the automatically mode where the program will randomly select one element from each column and combine them. The second mode is the user is presented...
PYTHON Suppose you need to import a single class named GeneTools from a module named bioinformatics,...
PYTHON Suppose you need to import a single class named GeneTools from a module named bioinformatics, in Python. Which of the following represents the correct use of an import statement? import GeneTools from bioinformatics from bioinformatics import GeneTools from bioinformatics import * import GeneTools
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) =...
1.) Using excel. A random number generator picks a number from one to nine in a...
1.) Using excel. A random number generator picks a number from one to nine in a uniform manner. X ~ _________ Graph the probability distribution. f(x) = _________ μ = _________ σ = _________ P(3.5 < x < 7.25) = _________ P(x > 5.67) P(x > 5|x > 3) = _________ Find the 90th percentile. 2) using excel A subway train on the Red Line arrives every eight minutes during rush hour. We are interested in the length of time...
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) =...
Question 1. Go to random.org. This website is a random number generator. Use it to generate...
Question 1. Go to random.org. This website is a random number generator. Use it to generate three numbers a, b, c between -10 and 10. Now let your a, b and c be the coefficients of the quadratic function f(x)=ax2 +bx+c. (For example, if the numbers you generated happened to be a = 2,b = 12, c = −1, your function for the rest of the question would bef(x) = 2x2 +12x−1.) (a) Put f(x) into “standard” or “vertex” formf(x)=a(x−h)2...
A computer random number generator was used to generate 950 random digits from 0 to 9....
A computer random number generator was used to generate 950 random digits from 0 to 9. The observed frequencies of the digits are given in the table below. 0 1 2 3 4 5 6 7 8 9 88 82 97 84 87 87 95 93 90 147 Using a 0.05significance level, test the claim that all of the digits are equally likely. (a) Find the rejection region. Reject H0 if χ2> (b) Find the test statistic. (Round your final...
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...
Python program.  from random import . Write a function called cleanLowerWord that receives a string as a...
Python program.  from random import . Write a function called cleanLowerWord that receives a string as a parameter and returns a new string that is a copy of the parameter where all the lowercase letters are kept as such, uppercase letters are converted to lowercase, and everything else is deleted. For example, the function call cleanLowerWord("Hello, User 15!") should return the string "hellouser". For this, you can start by copying the following functions discussed in class into your file: # Checks...
Random Number Guessing Game (python) *use comments Write a program guess.py that generates a random number...
Random Number Guessing Game (python) *use comments Write a program guess.py that generates a random number in the range of 1 through 20, and asks the user to guess what the number is. If the user's guess is higher than the random number, the program should display "Too high, try again." If the user's guess is lower than the random number, the program should display "Too low, try again." If the user guesses the number, the application should congratulate the...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT