<jupyter_start><jupyter_text>The numpy.random packageNumpy.random is a submodule of the Python library called Numpy. This submodule is a tool for creating random numbers, which can be used for simulating data. The Mersenne Twister algorithm in numpy.random is used to generate pseudorandom numbers. The Mersenne Twister is one of the most extensively tested random number generators in existence.  Simple Random DataThere are a number of simple random data functions included in the numpy.random module which can be used for creating random numbers of varying types. In this section I will explain the following four simple random data functions using examples and graphs:- numpy.random.rand- numpy.random.randn- numpy.random.randint- numpy.random.choice  numpy.random.randCreates a random array of positive floating point numbers in a given shape. The output values are in the interval [0,1). The square bracket informs us that it can return zero and the round bracket tells us that it can never return 1. This function represents a uniform distribution which means the numbers are equally spread out over the interval of [0, 1).<jupyter_code># import numpy library
import numpy as np
# create an array of random numbers with 4 rows and 2 columns
np.random.rand(4,2)
# create an array of 10 random numbers 
np.random.rand(10)
# import matplotlib library
import matplotlib.pyplot as plt
# create an array of 100 random numbers
x = np.random.rand(100)
x
# plot a histogram of the x values generated randomly by the np.random.rand(100) function
plt.hist(x)<jupyter_output><empty_output><jupyter_text>numpy.random.rand histogramThe histogram above shows a uniform distribution where the random numbers are equally spread over the interval [0, 1).  numpy.random.randnCreates a random array of floating point numbers which represent a normal distribution. The values are described by two numbers - the mean and the variance. The mean value is zero and the variance is 1.<jupyter_code># create an array of 200 floating point numbers
x = np.random.randn(200)
x
# plot a histogram of the x values generated randomly by the np.random.randn function
plt.hist(x)<jupyter_output><empty_output><jupyter_text>numpy.random.randn histogramThe histogram shows a normal distribution where the numbers are centred around the mean of zero and the variance is 1. This is known as the bell shaped curve. When I first created an array using the numpy.random.randn function I only used a sample size of 50. This lower sample size did not represent the bell shaped curve as well as i expected so I increased the sample size to 200 which produced a better shape as shown above.  numpy.random.randintThis function creates a random integer N where a <= N < b. The randint function accepts two parameters a lowest number and a highest number.<jupyter_code># use the random.randint function to pick 10 integers between 1 and 6
x = np.random.randint(1, 7, size=10)
x<jupyter_output><empty_output><jupyter_text>The numpy.random.randint function returns integers from a discrete uniform distribution. This function is often used to generate random rolls from a fair 6 sided dice.  numpy.random.choiceThe choice function can be used to generate a random element from a given list. The elements can be numbers or strings.<jupyter_code># use the random.choice function to randomly select a piece of fruit
fruit = ['Apple', 'Banana', 'Pear']
x = np.random.choice(fruit)
x
# use the random.choice function to randomly select a number in a list
numbers = [1, 2, 3, 4]
x = np.random.choice(numbers)
x<jupyter_output><empty_output><jupyter_text>The numpy.random.choice function can be used for a wide range of examples. The Python documentation uses an example whereby the colours on a roulette are provided as a list and picked at random or another example I've seen whilst researching this function online is the rock, paper, scissors game.  DistributionsThere are a number of distribution functions included in the numpy.random module. In this section I will explain the following five distribution functions using examples and graphs:- numpy.random.uniform- numpy.random.normal- numpy.random.poisson- numpy.random.chisquare- numpy.random.binomial  numpy.random.uniformReturns a random array of numbers with a uniform distribution, where the output interval [low, high) includes the low value but excludes the high value.<jupyter_code># create an array where x >= 0, x < 5 with 5000 elements.
x = np.random.uniform(0, 5, 5000)
x
# plot a histogram for the function np.random.uniform with 20 bins
plt.hist(x, bins=20)<jupyter_output><empty_output><jupyter_text>numpy.random.uniform histogramThe histogram shows a uniform distribution where the data follows a uniform pattern. The bins of the histogram are approximately the same size. When I first created an array using the numpy.random.uniform function I only used a sample size of 500. This lower sample size did not represent the uniform pattern as well as i expected so I increased the sample size to 5000 which produced a better shape as shown above.<jupyter_code>np.all(x >= 0)
np.all(x < 5)
("Size of the array: ", x.size)<jupyter_output><empty_output><jupyter_text>numpy.random.normalThe numpy.random.normal function returns a random array of numbers which are defined by two numbers, the mean and the standard deviation. This function produces a normal distribution also known as the bell shaped curve. The bell shaped curve peaks at its mean and the standard deviation describes how wide or narrow the curve is.<jupyter_code># create an array centred around zero, standard deviation or spread of 0.2 and 200 elements
x = np.random.normal(0, 0.2, 200)
x
# plot a histogram for the function np.random.normal with 10 bins
plt.hist(x)<jupyter_output><empty_output><jupyter_text>numpy.random.normal histogramThe histogram shows a normal distribution where the data shows a pattern known as the bell shaped curve. In a normal distribution, points are as likely to occur on one side of the average as on the other. The 200 values of x are centred around 0 with a spread of 0.2.  numpy.random.poissonThe numpy.random.poisson function draws samples from a poisson distribution. The poisson distribution is a discrete probability distribution where each event is independent of the last event. The poisson distribution is useful for measuring the number of times an event occurs within a particular time interval.<jupyter_code># create an array of 1000 elements with an interval of 5
x = np.random.poisson(5, size=1000)
x
# plot a histogram for the poisson distribution
plt.hist(x, bins=20)
# calculate the variance of the array
np.var(x)
# calculate the mean of the array
np.mean(x)<jupyter_output><empty_output><jupyter_text>numpy.random.poisson histogramThe histogram shows that the random elements from the array peak at 5 as we set our interval at 5 - this is the expected value hence should be the greatest out of the sample size of 1000. The mean and the variance are almost equal at 4.617 and 4.966 respectively - the numpy.random.poisson function has created an array of numbers where the average number and how far numbers are spread out from the mean are almost identical.  numpy.random.gammaThe numpy.random.gamma function produces samples from a gamma distribution. The parameters must be greater than 0 in the format (shape (k), scale (theta), Size). It produces continuous variables that are positive and have skewed distributions. The gamma distribution is often used to find the time required to complete a specific task.<jupyter_code># use the np.random.gamma function where the shape is 2, scale is 1 and size of the random sample is 10000
x = np.random.gamma(2, 1, 10000)
x
# create a histogram of the numpy.random.gamma function with 20 bins
plt.hist(x, bins=20)<jupyter_output><empty_output><jupyter_text>numpy.random.gamma histogramThe gamma histogram above shows a skewed continuance probability distribution. If the number of events (shape) is 1 and theta is 2 (the mean waiting time between events) the graph shifts to the right and shows that waiting times between events are increasing.  numpy.random.binomialThe numpy.random.binomial function draws samples from a binomial distribution. This type of distribution is a discrete probability distribution which consists of a number of trials with only two possible outcomes for each trail - success or failure for example. The trails are independent - the outcome of one trail does not affect another trail. The binomial distribution is often used for the experiment of tossing a coin where there are two possible outcomes - heads or tails.<jupyter_code># create a function where n=1 (heads), p = 0.5 (as 2 possible outcomes heads or tails) and sample size is 100. 
# If heads is 1, tails is 0.
x = np.random.binomial(n=1, p=0.5, size=100)
x
# calculate the mean of 1 of the array
np.mean(x)
# plot a histogram for the array
plt.hist(x)<jupyter_output><empty_output><jupyter_text>numpy.random.binomial histogramThe histogram shows the two outcomes 0 for tails and 1 for heads. In this instance, in the randomly selected array, tails was more successful than heads when tested 100 times.   Permutations FunctionsThere are two permutation functions:- numpy.random.shuffle- numpy.random.permutation  numpy.random.shuffleModifies an array of numbers by shuffling it's elements.<jupyter_code># create a list of 9 numbers from 0 to 8
x = np.arange(9).reshape (3, 3)
x
# modify the list of 9 numbers
np.random.shuffle(x)
x<jupyter_output><empty_output><jupyter_text>numpy.random.permutationAlters the order of an array of numbers<jupyter_code># create a list of 9 numbers from 0 to 8
x = np.arange(9).reshape (3, 3)
x
# alter the order of the list of 9 numbers
np.random.permutation(x)<jupyter_output><empty_output><jupyter_text>The difference between the numpy.random.shuffle function and the numpy.random.permutation function is the random.shuffle function only shuffles the columns of the array and not the rows, whereas the random.permutation only permutes the rows of the array not the columns.    Seeds  numpy.random.seedThe numpy.random.seed function makes the random array of numbers drawn predictable if the seed is reset each time the random function is run. If the seed is not reset the numbers drawn will be unpredictable - they will change.<jupyter_code># set the seed
np.random.seed(4)
# create a random array of 4 integers between 1 and 5 
x = np.random.randint(1, 6, size=4)
x
# set the seed
np.random.seed(4)
# create a random array of 4 integers between 1 and 5
x = np.random.randint(1, 6, size=4)
x<jupyter_output><empty_output><jupyter_text>As you can see from the above example, due to the fact that the seed has been reset each time the np.random.randint function is run the array of numbers produced are the same.<jupyter_code># set the seed
np.random.seed(4)
# create a random array of 4 integers between 1 and 5 
x = np.random.randint(1, 6, size=4)
x
# create a random array of 4 integers between 1 and 5 
x = np.random.randint(1, 6, size=4)
x<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.05560068598248939, 'fraction_numerical': 0.018322953335138552, 'mean_word_length': 4.865537321334039, 'pattern_counts': {'":': 0, '<': 61, '<?xml version=': 0, '>': 59, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '14', 'dir': 'jupyter-structured-clean-dedup', 'id': '604172', 'license': 'Apache-2.0', 'n_tokens_mistral': 3120, 'n_tokens_neox': 2873, 'n_words': 1556, 'path': 'numpy-random.ipynb', 'repo_name': 'beccycole/Programming-for-Data-Analysis', 'size': '11079'}