How create random matrix with values in a specific range?

时间:2016-09-01 06:20:44

标签: matlab matrix random

rand(3,4)

generates the following 3x4 matrix for me:

0.4229    0.4709    0.6385    0.3196
0.0942    0.6959    0.0336    0.5309
0.5985    0.6999    0.0688    0.6544

How can I limit the values generated to a specific range? Like restricting them to values between 1 and 100. And how can I specify whether I want the random numbers to be float or int?

1 个答案:

答案 0 :(得分:6)

You can multiply by the range you want, and add the minimum value. So, to get a matrix with values in the range [1, 100], do:

1 + 99*rand(3,4)

If you want single or something else, use the typename argument to specify this.

If you want integers, use randi:

1 + randi(99,3,4)