How do I generate a random number between two numbers, "n" number of times?
5 次查看(过去 30 天)
显示 更早的评论
I need to generate a random number that is between two other numbers and this will be looped "n" number of times in a "for" loop.
0 个评论
回答(2 个)
Matthew Eicholtz
2016-10-18
Without using a for-loop:
a = 0;
b = 255;
n = 1000;
x = a + (b-a).*rand(n,1);
will generate n numbers randomly from the interval [a,b]. You could do this with a for-loop as well, but it is unecessary:
x = zeros(n,1);
for ii=1:n
x(ii) = a + (b-a).*rand;
end
0 个评论
Geoff Hayes
2016-10-18
Uriel - see rand and in particular the Random Numbers Within Specified Interval example. You could create an array of n elements and then populate each element (as you iterate in your for loop) with the random number as per the example.
0 个评论
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Loops and Conditional Statements 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!