Random number generator between two numbers - MatL

2019-01-26 01:28发布

I am trying to create a random number generator between two numbers in MatLab but I am unable to figure out the correct equation.

I know that you can use the rand() function to create random numbers but what if I wanted to have numbers only generated between -.5 and .5? Is this possible to accomplish in MatLab?

标签: matlab random
3条回答
孤傲高冷的网名
2楼-- · 2019-01-26 01:29

it is easy if you want get a random value between -.5~.5 here as an example

rand(1)-.5

more generally, to generate random value between [a b] you can use a generator like this

rand(1)×(b-a)+a
查看更多
We Are One
3楼-- · 2019-01-26 01:34

The simple way to do that is use

randi([min, max])  
查看更多
Rolldiameter
4楼-- · 2019-01-26 01:39

The answer for adjusting the interval is listed in the help section on rand() :

Example 1

Generate values from the uniform distribution on the interval [a, b]: r = a + (b-a).*rand(100,1);

查看更多
登录 后发表回答