How can I generate a random number that is negative or positive and between two other values?
207 次查看(过去 30 天)
显示 更早的评论
I need to write a function that generates two numbers that are between the negative and positive values of an integer. For example, if I call the function and input 7 it should generate two random numbers between -7 and +7. I can't figure out a way to use the randi function because the values I need can't be just integers.
0 个评论
采纳的回答
Walter Roberson
2015-11-20
Hint: rand()-1/2 is in the range -1/2 to +1/2
1 个评论
Jacob
2023-10-11
if you a number x
generating random between -x and x
x = 7;
for i = 1:x*2
z(i) = (2*x)*(rand(1)-.5);
end
disp(z)
更多回答(2 个)
Rahul Gulia
2020-1-22
>> randi([-2,2],3,3) will give the desired output.
Thanks for pointing out my mistake @ Walter Roberson .
0 个评论
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Random Number Generation 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!