import numpy as np
L = np.random.normal(1.0, 0.005, 100)
uniformrandom()
is not returning anything. You need to add a return
statement:
def uniformrandom(n):
i = 0
while i < n:
gauss(1.0, 0.005)
i = i + 1
return i
That is returning the number of gauss
's, though. You aren't even storing the gauss()
calls in a list. You should change your function to this:
def uniformrandom(n):
gausses = []
for _ in range(n):
gausses.append(gauss(1.0, 0.005))
return gausses
You could even use a list comprehension:
def uniformrandom(n):
return [gauss(1.0, 0.005) for _ in range(n)]
For example, to generate a single suitable number:
def uniformrandom_1():
return gauss(1.0, 0.005)
now, if you want a list of n
numbers, you can just use
[uniformrandom_1() for i in range(n)]
or write that as a function:
def uniformrandom(n):
return [uniformrandom_1() for i in range(n)]
Try the following code,
def uniformrandom(n):
nums = []
total = 0
i = 0
for i in range(1, n):
num = gauss(1.0, 0.005)
nums.append(num)
total += num
return (nums, total / n)
This will create a list with n random numbers using gauss. import random as rd
def uniformrandom(n):
i = 0
random_list = [] * n
for i in range(n):
random_list += [rd.gauss(1, 0.005)]
return random_list
In order to generate 100 normally distributed random numbers in Python by using the function gauss with expectation 1.0 and standard deviation 0.005, one can use numpy.random.normal
as follows
import numpy as np
random_numbers = np.random.normal(1.0, 0.005, 100)
In order to store the random_numbers
in an array, one can do that with numpy.array
as follows
random_numbers_array = np.array(random_numbers)
Then to calculate the mean use numpy.mean
mean = np.mean(random_numbers_array)
A function, that takes as input the mean and std of the random number generator, and does what OP wants, can be something like
def uniformrandom(mean, std):
random_numbers = np.random.normal(mean, std, 100)
random_numbers_array = np.array(random_numbers)
mean = np.mean(random_numbers_array)
std = np.std(random_numbers_array)
return random_numbers_array, mean, std
Let's see what it retrieves
print(uniformrandom(1.0, 0.005))
[Out]:
(array([1.00716042, 0.99938042, 0.99178698, 1.00791888, 1.00623344,
1.00555578, 0.99890757, 1.00695046, 0.98482516, 0.9928371,
1.00016377, 0.99510195, 1.00280951, 0.99472607, 0.99453582,
1.00791222, 1.00302319, 1.00004503, 0.99884054, 1.00429994,
0.99591756, 1.010769, 1.00827643, 0.996754, 0.99236853,
1.00096622, 1.00092158, 1.00192217, 1.00148108, 0.9975529,
1.00953799, 1.0073464, 0.99942883, 1.0065139, 1.00265884,
1.00885268, 0.99613224, 1.00299541, 0.99977556, 1.01090735,
1.00132776, 0.99711267, 1.00129545, 1.00500702, 0.99937595,
1.00603761, 0.98960716, 0.99932355, 0.99687272, 1.00332839,
0.991147, 0.99643908, 0.99279811, 1.00112179, 1.00012058,
0.9989405, 1.00150169, 1.00683601, 0.99885708, 0.99632519,
1.00112315, 0.99280336, 1.00759542, 1.00140661, 1.00183764,
0.99540866, 1.0002343, 0.99421579, 1.01169739, 1.00330142,
0.99977923, 1.00365608, 0.9984007, 1.00106568, 1.00349778,
0.99527499, 0.99189253, 0.99477082, 0.99486919, 0.99784054,
0.99240925, 1.00417557, 0.99566904, 1.00355492, 0.99717846,
0.99910477, 0.99718301, 1.00711659, 0.99623698, 1.00143697,
1.00876763, 1.0049953, 0.99885742, 0.99498201, 1.00324752,
0.99907905, 0.99762597, 0.99502917, 0.99511507, 1.00991401
]), 1.0002981820807302, 0.005332038881947385)
To generate five random numbers from the normal distribution we will use numpy.random.normal() method of the random module. ,Writing code in comment? Please use ide.geeksforgeeks.org, generate link and share the link here.,In Numpy we are provided with the module called random module that allows us to work with random numbers. The random module provides different methods for data distribution. In this article, we have to create an array of specified shape and fill it random numbers or values such that these values are part of a normal distribution or Gaussian distribution. This distribution is also called the Bell Curve this is because of its characteristics shape.,CS SubjectsMathematicsOperating SystemDBMSComputer NetworksComputer Organization and ArchitectureTheory of ComputationCompiler DesignDigital LogicSoftware Engineering
Output :
[0.27491897 - 0.18001994 - 0.01783066 1.07701319 - 0.11356911]
Draw random samples from a normal (Gaussian) distribution.,The normal distributions occurs often in nature. For example, it describes the commonly occurring distribution of samples influenced by a large number of tiny, random disturbances, each with its own unique distribution [2].,Drawn samples from the parameterized normal distribution.,The probability density function of the normal distribution, first derived by De Moivre and 200 years later by both Gauss and Laplace independently [2], is often called the bell curve because of its characteristic shape (see the example below).
>>> mu, sigma = 0, 0.1 # mean and standard deviation >>> s = np.random.normal(mu, sigma, 1000)
>>> abs(mu - np.mean(s)) 0.0 # may vary
>>> abs(sigma - np.std(s, ddof = 1)) 0.1 # may vary
>>>
import matplotlib.pyplot as plt >>>
count, bins, ignored = plt.hist(s, 30, density = True) >>>
plt.plot(bins, 1 / (sigma * np.sqrt(2 * np.pi)) *
...np.exp(-(bins - mu) ** 2 / (2 * sigma ** 2)),
...linewidth = 2, color = 'r') >>>
plt.show()
>>> np.random.normal(3, 2.5, size = (2, 4)) array([ [-4.49401501, 4.00950034, -1.81814867, 7.29718677], # random[0.39924804, 4.68456316, 4.99394529, 4.84057254] ]) # random
Random numbers from a normal distribution can be generated using runif() function.,Random numbers from a normal distribution can be generated using rnorm() function.,For example, runif() generates random numbers from a uniform distribution and rnorm() generates from a normal distribution.,We need to specify how many numbers we want to generate.
Example: Uniform Distribution
> runif(1) # generates 1 random number[1] 0.3984754 > runif(3) # generates 3 random number[1] 0.8090284 0.1797232 0.6803607 > runif(3, min = 5, max = 10) # define the range between 5 and 10[1] 7.099781 8.355461 5.173133
Example: Normal Distribution
> rnorm(1) # generates 1 random number[1] 1.072712 > rnorm(3) # generates 3 random number[1] - 1.1383656 0.2016713 - 0.4602043 > rnorm(3, mean = 10, sd = 2) # provide our own mean and standard deviation[1] 9.856933 9.024286 10.822507
In this chapter we introduce how to generate random numbers from various distributions in Python. The core library we will be using is the numpy library which contains a powerful random number generator. We will then draw random numbers from different distributions and graph these numbers using histograms in order to check the shape of the distribution that these numbers follow.,Here is another example of two normally distributed random variables. The first random variable has a smaller standard deviation than the second. Let us see how the distribution of these two compare.,Draw 100 random numbers from a Standard Normal distribution,We next make a histogram of these numbers to see the shape of the distribution where they come from. I set the number of bars in the histogram to 30 in order to get a nice picture.
import numpy as np import matplotlib.pyplot as plt import math from scipy import stats as st import time # Imports system time module to time your script plt.close('all') # close all open figures tic = time.time()
myNumber = np.random.uniform(0, 1)
print('My first random number in [0,1] is {}'.format(myNumber))
My first random number in [0, 1] is 0.8602692305307852
myNumber = np.random.uniform(0, 1)
print('My second random number in [0,1] is {}'.format(myNumber))
My second random number in [0, 1] is 0.42116143252571125
np.random.seed(123456789)
myNumber = np.random.uniform(0, 1)
print('My first random number in [0,1] is {}'.format(myNumber))