Functions > Statistics > Probability Distributions > Example: Generating Random Numbers
  
Example: Generating Random Numbers
Create vectors of random numbers that are respectively uniformly distributed, normally distributed, or exponentially distributed over an interval.
Uniformly Distributed
1. Call function runif to generate n_set random numbers that are uniformly distributed between low and high.
Click to copy this expression
Click to copy this expression
Click to copy this expression
Click to copy this expression
2. Use function hist to generate a histogram of the uniform set with n_bins bins, then calculate the mean of the set.
Click to copy this expression
Click to copy this expression
Click to copy this expression
Click to copy this expression
Click to copy this expression
3. Plot the histogram and show the expected mean for the data set:
Click to copy this expression
* 
To get a new set of random numbers, on the Calculation tab, in the Controls group, click Calculate. A new seed value is used to generate random numbers each time you recalculate.
Normally Distributed
1. Call function rnorm to generate n_set random numbers having the normal distribution with mean μ and standard deviation σ.
Click to copy this expression
Click to copy this expression
Click to copy this expression
2. Use functions floor, ceil, min and max to calculate the minimum and the maximum range of the data set, then calculate the width of each bin:
Click to copy this expression
Click to copy this expression
Click to copy this expression
3. Set the horizontal scale for the plot:
Click to copy this expression
Click to copy this expression
4. Call function dnorm to calculate the normal distribution F expected for the data set, then plot a histogram of the normal set with n_bins bins.
Click to copy this expression
Click to copy this expression
Click to copy this expression
Exponentially Distributed
1. Call function rexp to generate a set of random numbers that are exponentially distributed with rate r.
Click to copy this expression
Click to copy this expression
2. Call function dexp to calculate the exponential distribution F expected for the data set, then plot a histogram of the exponential set with n_bins bins.
Click to copy this expression
Click to copy this expression
Click to copy this expression
Click to copy this expression
Click to copy this expression
Click to copy this expression
Click to copy this expression
Click to copy this expression
3. Call functions pexp and qexp to calculate and plot the cumulative probability distribution for value x and the inverse cumulative probability distribution for value p, respectively.
Click to copy this expression
Click to copy this expression
Click to copy this expression
The first argument of function qexp is probability and must be less than 1, hence the division of int by 100.