recipes : programming : Generating random numbers

Problem

You want to generate some random numbers.

Solution

There are various ways of doing this. Here are some examples.

%produce 10 random numbers distrubuted between 0 and 1
rand(1,10)

%produce 10 random numbers drawn from a normal distribution 
% with a mean of zero and a standard deviation of 1.
randn(1,10)

%produce 50 random numbers drawn from a normal distribution 
% with a mean of zero and a standard deviation of 5.
randn(1,50)*5

%arrange the numbers 1 to 10 in a random order
randperm(10)

Discussion

Generating random numbers is useful for many purposes such as jitter data on plots, bootstrapping and permutation tests, generating "fake" data for testing purposes, etc.