Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Generate a random number in a certain range in MATLAB

How can I generate a random number in MATLAB between 13 and 20?

like image 939
crowso Avatar asked Feb 22 '11 11:02

crowso


People also ask

How do you generate a random number from 1 to 10 in MATLAB?

x=randi([1,10],1,10); Sign in to comment. Sign in to answer this question.

How do you generate one random number in MATLAB?

rand(100,1) means create a random matrix of size [100x1] of values on the open interval of (0,1).

How do you generate a random number between 0 and 1 in MATLAB?

The rand function returns floating-point numbers between 0 and 1 that are drawn from a uniform distribution. For example: rng('default') r1 = rand(1000,1); r1 is a 1000-by-1 column vector containing real floating-point numbers drawn from a uniform distribution.


4 Answers

If you are looking for Uniformly distributed pseudorandom integers use:

randi([13, 20])
like image 106
zellus Avatar answered Oct 17 '22 15:10

zellus


http://www.mathworks.com/help/techdoc/ref/rand.html

n = 13 + (rand(1) * 7);
like image 22
TimCodes.NET Avatar answered Oct 17 '22 14:10

TimCodes.NET


r = 13 + 7.*rand(100,1);

Where 100,1 is the size of the desidered vector

like image 6
pcofre Avatar answered Oct 17 '22 14:10

pcofre


ocw.mit.edu is a great resource that has helped me a bunch. randi is the best option, but if your into number fun try using the floor function with rand to get what you want.

I drew a number line and came up with

floor(rand*8) + 13
like image 1
ash_bobham Avatar answered Oct 17 '22 15:10

ash_bobham