We learned in the last tutorial about random numbers. We will just do a bit more on this now.
Generating a random number in MATLAB is easy. Try the following code:
clc;
clear all;
close all;
rng('shuffle');
for counter = 1:1000000
my_numbers(counter) = rand(1);
end
hist(my_numbers);
This code generates one million random numbers between 0 and 1 and puts them in a variable called my_numbers. The last line generates our first plot - a histogram of my_numbers. Because rand generates numbers from a uniform distribution the histogram will be uniform and not Gaussian. More on this below.
rand is a very powerful command. If you wanted to generate 1,000,000 random number you actually do not need the loop, you could simply go:
my_numbers = rand(1000000,1);
This tells rand to generate 1000000 random numbers between 0 and 1. The reason it is (1000000,1) is because MATLAB likes to think in terms of matrices so you have to tell MATLAB this is a matrix with 1000000 rows but only 1 column.
If you want to specify the range of the random numbers (say 20 to 40) you could do the following:
my_numbers = 20 + 20 * rand(1000000,1);
This tells MATLAB to generate 1000000 random numbers with a range of 0 to 1. However, if we multiply this vector by 20 and then add 20 this will make all the numbers between 20 and 40.
If you want the numbers to be integers it is even easier - we can use randi as opposed to rand. The following command generates 1000000 random integers between 4 and 12:
my_numbers = randi([4 12],1000000,1);
Note, it is a quirk of MATLAB but this nice way of specifying the range does not work with the rand command.
Last of all, it is easy to generate numbers from a normal distribution. We can use randn. Try the following code.
my_numbers = 300 + 50 * randn(1000000,1);
hist(my_numbers);
This creates 1000000 random numbers with a mean of 300 and a standard deviation of 50. hist is used to verify this.
That is quite a bit on random numbers. Note, you can always use help to learn more. Move onto the next tutorial now.
Generating a random number in MATLAB is easy. Try the following code:
clc;
clear all;
close all;
rng('shuffle');
for counter = 1:1000000
my_numbers(counter) = rand(1);
end
hist(my_numbers);
This code generates one million random numbers between 0 and 1 and puts them in a variable called my_numbers. The last line generates our first plot - a histogram of my_numbers. Because rand generates numbers from a uniform distribution the histogram will be uniform and not Gaussian. More on this below.
rand is a very powerful command. If you wanted to generate 1,000,000 random number you actually do not need the loop, you could simply go:
my_numbers = rand(1000000,1);
This tells rand to generate 1000000 random numbers between 0 and 1. The reason it is (1000000,1) is because MATLAB likes to think in terms of matrices so you have to tell MATLAB this is a matrix with 1000000 rows but only 1 column.
If you want to specify the range of the random numbers (say 20 to 40) you could do the following:
my_numbers = 20 + 20 * rand(1000000,1);
This tells MATLAB to generate 1000000 random numbers with a range of 0 to 1. However, if we multiply this vector by 20 and then add 20 this will make all the numbers between 20 and 40.
If you want the numbers to be integers it is even easier - we can use randi as opposed to rand. The following command generates 1000000 random integers between 4 and 12:
my_numbers = randi([4 12],1000000,1);
Note, it is a quirk of MATLAB but this nice way of specifying the range does not work with the rand command.
Last of all, it is easy to generate numbers from a normal distribution. We can use randn. Try the following code.
my_numbers = 300 + 50 * randn(1000000,1);
hist(my_numbers);
This creates 1000000 random numbers with a mean of 300 and a standard deviation of 50. hist is used to verify this.
That is quite a bit on random numbers. Note, you can always use help to learn more. Move onto the next tutorial now.