When considering generating random numbers from normal distribution, we should first know the mean and variance (standard deviation) of normal distribution. With these, we can call existing modules and functions in python to generate random numbers. The random.normal function in the Numpy module is called here. Because the nonparametric logic is simple, all the direct paste codes are as follows:
Import numpy as np# defines the function defget _ normal _ random _ number (loc, Scale): "":paramloc: mean value of normal distribution: paramscale: standard deviation of normal distribution: return: random number generated by normal distribution ""# Random number generated by normal distribution = np.random.normal (loc = loc, Scale = scale) # Return value number # Main module if _ _ name _ _ = "_ _ main _ _": # Function call n = get _ normal _ random _ number (LOC = 2, scale = 2) # Print result (n) # Result
Obtain the function of random number from the uniform distribution of given parameters.
When considering obtaining random numbers from uniform distribution, we should know the upper and lower bounds of uniform distribution in advance, and then call the random.uniform function of Numpy module to generate random numbers.
Import numpy as np# defines the function defget _ uniform _ random _ number (low, High): "":paramlow: lower bound of uniform distribution: paramhigh: upper bound of uniform distribution: return: random number generated by uniform distribution ""# Random number generated by uniform distribution = np.random.uniform (low, High)# Return value number # Main module if _ _ name _ _ = "_ _ main _ _": # Function call n = get _ uniform _ random _ number (low = 2, high = 4) # Print result (n) # Result: 2.44444.
3 Generate random numbers according to the specified probability.
Sometimes we need to generate random numbers according to the specified probability, such as knowing the proportion of each color ball in the box and guessing the color of the ball to be taken out next time. The problem introduced here is similar to the above example. It is necessary to give a probability list and generate random numbers from the number list or interval list corresponding to the list. It is discussed in two parts.
3. 1 Randomly select numbers from the number list according to the specified probability.
Given a number list and a corresponding probability list, how to extract random numbers from the number list according to these known conditions? Here we consider using uniform distribution to simulate probability, and the code is as follows:
Import numpy as npimport random# defines the function defget _ uniform _ random _ number (low, High): "":paramlow: lower bound of uniform distribution: paramhigh: upper bound of uniform distribution: return: random number generated by uniform distribution ""# Random number generated by uniform distribution = np.random.uniform (low, High)# return value number # defines the function defget _ number _ by _ pro (number _ list, Pro _ list): "":paramnumber _ list: list of numbers: parampro _ list: list of probabilities corresponding to numbers: return: numbers extracted from the list of numbers by probability ""# simulation probability x = random.uniform (0, 1) # Cumulative probability cum _ pro = 0.0 # Packages iterable objects into a tuple list for number, number _ pro in zip (number _ list, pro _ list): cum _ pro+= number _ pro if x.
3.2 Generate a random number from an interval in the interval list according to the specified probability.
Given an interval list and a corresponding probability list, a tuple composed of elements in corresponding positions in the two lists represents the probability that a certain number appears in an interval. Given these, how do we generate random numbers? Here we achieve our goal by using uniform distribution twice. The code is as follows:
Import numpy as npimport random# defines the function defget _ uniform _ random _ number (low, High): "":paramlow: lower bound of uniform distribution: paramhigh: upper bound of uniform distribution: return: random number generated by uniform distribution ""# Random number generated by uniform distribution = np.random.uniform (low, High)# return value number # defines the function defget _ number _ by _ pro (number _ list, Pro _ list): "":paramnumber _ list: list of numbers: parampro _ list: list of probabilities corresponding to numbers: return: numbers extracted from the list of numbers by probability ""# simulation probability x = random.uniform (0, 1) # Cumulative probability cum _ pro = 0.0 # Packages iterable objects into a tuple list for number, number _ pro in zip (number _ list, pro _ list): cum _ pro+= number _ pro if x.