Generate a random number in a certain range in MAT

2019-01-07 14:19发布

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

9条回答
时光不老,我们不散
2楼-- · 2019-01-07 14:45

Best solution is randint , but this function produce integer numbers.

You can use rand with rounding function

  r = round(a + (b-a).*rand(m,n));

This produces Real random number between a and b , size of output matrix is m*n

查看更多
\"骚年 ilove
3楼-- · 2019-01-07 14:53

if you are looking to generate all the number within a specific rang randomly then you can try ` r = randi([a b],1,d)

a=start point b=end point d= how many number you want to generate but keep in mind that d should be less than or equal to b-a

查看更多
相关推荐>>
4楼-- · 2019-01-07 14:56

You can also use:

round(mod(rand.*max,max-1))+min
查看更多
神经病院院长
5楼-- · 2019-01-07 14:58

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
查看更多
SAY GOODBYE
6楼-- · 2019-01-07 15:00

Generate values from the uniform distribution on the interval [a, b].

      r = a + (b-a).*rand(100,1);
查看更多
我欲成王,谁敢阻挡
7楼-- · 2019-01-07 15:02

If you are looking for Uniformly distributed pseudorandom integers use:

randi([13, 20])
查看更多
登录 后发表回答