필터 지우기
필터 지우기

how to create random double in specific range?

조회 수: 43 (최근 30일)
fred bnm
fred bnm 2016년 12월 5일
편집: Image Analyst 2023년 4월 1일
HI, randi function Can only generate integers in specific range.
num = randi([1,3],[1,10],'double');
how to create double numbers in range such as [0.2,1.2]?

채택된 답변

Cyrus
Cyrus 2016년 12월 5일
편집: Image Analyst 2023년 4월 1일
For generating double numbers you can use:
r = rand( 1, 3 ,'double')
r = 1×3
0.8499 0.2298 0.4419
and to have them in a specific range you can use the following source: https://www.mathworks.com/help/matlab/math/floating-point-numbers-within-specific-range.html
which is:
a = 50;
b = 100;
r = (b-a).*rand(1000,1) + a;
thus, finally:
numElements = 10;
a = 0.2;
b = 1.2;
r= (b-a).*rand(1, numElements, 'double') + a; % [SL: added the missing "+a" term]
The result:
r
r = 1×10
0.3118 1.1751 0.8208 0.8696 0.6526 1.1281 0.4074 0.5364 0.2122 0.9621

추가 답변 (1개)

Carolina Escobar
Carolina Escobar 2023년 4월 1일
e = rand(0.1,1)
  댓글 수: 1
Steven Lord
Steven Lord 2023년 4월 1일
This will throw an error.
e = rand(0.1, 1)
Error using rand
Size inputs must be integers.
As the error message indicates, the size inputs to rand must contain integer values.

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

카테고리

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