Floating numbers generation in a range

I Want to generate an array of 100 Different random floating numbers in the range 2.2 to 22. Please help me with this.

답변 (2개)

Roger Stafford
Roger Stafford 2013년 9월 21일

0 개 추천

What kind of probability distribution do you wish these numbers to have over this interval? If it is to be uniform use 'rand' with appropriate scaling and offset.

댓글 수: 4

BISWA
BISWA 2013년 9월 21일
Thanks for responding. I had tried rand but the output was showing integers only.
Roger Stafford
Roger Stafford 2013년 9월 21일
I think you were using 'randi', not 'rand'. The 'randi' function returns only integers, whereas 'rand' returns fractional values. The probability of getting an integer output from 'rand' is exceedingly small. I have personally never seen one.
BISWA
BISWA 2013년 9월 21일
Please post the code here which will generate an array of 50 floating numbers between 2.2 to 22.
You never looked up rand() in the help did you? Otherwise you would have noticed Example 1 which solves your problem
Example 1
Generate values from the uniform distribution on the interval [a,b]:
r = a + (b-a).*rand(100,1);
and I'm pretty sure you would have figured out a is 2.2 and b is 22 and you wouldn't have needed to wait for Simon to do it for you. Please mark the answer as Accepted.

댓글을 달려면 로그인하십시오.

Simon
Simon 2013년 9월 21일

0 개 추천

19.8*rand(50, 1) +2 .2

댓글 수: 1

BISWA
BISWA 2013년 9월 21일
Thank you very much. Its working just fine.

댓글을 달려면 로그인하십시오.

카테고리

도움말 센터File Exchange에서 Creating and Concatenating Matrices에 대해 자세히 알아보기

질문:

2013년 9월 21일

Community Treasure Hunt

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

Start Hunting!

Translated by