Octave/Matlab: Generating Random Numbers In Range

In this tutorial we will learn how to generate random numbers in range using Octave/Matlab. I am going to be using Octave for illustration. The same commands will work in Matlab.

We will be using randi command for generating random numbers in range. The syntax is randi([start,end]).

For example, I want to generate a random number between -10 and 10. So I type randi([-10,10]) on the Octave command line



I get a random number between -10 and 10 as an output, In this case it was -4.



This tutorial was inspired by a question at http://stackoverflow.com/questions/5077800/generate-a-random-number-in-a-certain-range-in-matlab and a wonderful answer by http://stackoverflow.com/users/426834/zellus