generate random numbers in range from (0.8 to 4)

222 次查看(过去 30 天)
Hi How to generate 20 random numbers in range from (0.8 to 4) Thanks
  1 个评论
Jan
Jan 2017-11-6
编辑:Jan 2017-11-6
@Arnab: See Azzi's and Image Analyst's answers, which contains exactly the same.

请先登录,再进行评论。

采纳的回答

Azzi Abdelmalek
Azzi Abdelmalek 2013-3-11
编辑:Azzi Abdelmalek 2013-3-11
xmin=0.8
xmax=4
n=20
x=xmin+rand(1,n)*(xmax-xmin)
  3 个评论
Steven Lord
Steven Lord 2021-1-19
xmin=0.8;
xmax=4;
n = 1e6; % Generating a few more numbers than the original n=20;
x=xmin+rand(1,n)*(xmax-xmin);
histogram(x, 0.8:0.1:4.1)
Looks reasonably uniform to me.
Image Analyst
Image Analyst 2021-1-20
If you want it EXACTLY perfectly uniform (which is doubtful), then you should use linspace() instead of rand().

请先登录,再进行评论。

更多回答(3 个)

Image Analyst
Image Analyst 2013-3-11
Did you look in the help? You will see the first example does what you want:
Example 1
Generate values from the uniform distribution on the interval [a, b]:
r = a + (b-a).*rand(100,1);
  1 个评论
Jan
Jan 2013-3-11
编辑:Jan 2017-11-6
Suggesting to read the help text is a very strong idea, because it helps in nearly all future problems also.

请先登录,再进行评论。


ChristianW
ChristianW 2013-3-11
n = 20;
R = [0.8 4];
z = rand(n,1)*range(R)+min(R)

Ka Mirul
Ka Mirul 2017-10-17

Hallo, I have seen a tutorial about "Generating Random Number in Specific Range" at https://www.youtube.com/watch?v=MyH3-ONYL_k and it works. Hope it will help you.

类别

Help CenterFile Exchange 中查找有关 Random Number Generation 的更多信息

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by