how to create random double in specific range?
50 ビュー (過去 30 日間)
古いコメントを表示
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]?
0 件のコメント
採用された回答
Cyrus
2016 年 12 月 5 日
編集済み: Image Analyst
2023 年 4 月 1 日
For generating double numbers you can use:
r = rand( 1, 3 ,'double')
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
0 件のコメント
その他の回答 (1 件)
Carolina Escobar
2023 年 4 月 1 日
e = rand(0.1,1)
1 件のコメント
Steven Lord
2023 年 4 月 1 日
This will throw an error.
e = rand(0.1, 1)
As the error message indicates, the size inputs to rand must contain integer values.
参考
カテゴリ
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!