Random number between two decimal numbers

3 ビュー (過去 30 日間)
Evica Smilkoska
Evica Smilkoska 2022 年 4 月 15 日
コメント済み: Image Analyst 2022 年 4 月 16 日
Hi, how can a generate random number between two decimal numbers? For example i want to generate array 24x1 and for each row i have different interval, for the first row the interval is 0.124 to 0.908 and for second row 0.325 to 0.846. The generated values cant be above or below the interval.

採用された回答

Walter Roberson
Walter Roberson 2022 年 4 月 15 日
intervalmins = [0.124, 0.325];
intervalmaxs = [0.908, 0.846];
output = rand(length(intervalmins), 1) .* (intervalmaxs(:) - intervalmins(:)) + intervalmins(:)
output = 2×1
0.9058 0.4439
  1 件のコメント
Evica Smilkoska
Evica Smilkoska 2022 年 4 月 16 日
Thank you enormously!

サインインしてコメントする。

その他の回答 (1 件)

Image Analyst
Image Analyst 2022 年 4 月 15 日
Did you check the documentation. The second example is
Generate a 10-by-1 column vector of uniformly distributed numbers in the interval (-5,5).
r = -5 + (5+5)*rand(10,1)
  2 件のコメント
Evica Smilkoska
Evica Smilkoska 2022 年 4 月 16 日
I have tried this version but after 10000 iterations the min and max of the decimal intervals are not kept, and i get values lower than the min value of the interval.
Image Analyst
Image Analyst 2022 年 4 月 16 日
Strange, since it's essentially the same formula that you accepted in Walter's answer. You changed the numbers in the MATLAB example to what you wanted your range to be right? Either inside your loop or by setting up a vector with all the parameters in advance like Walter showed you.
I can't really say more since you never uploaded your code. But it sounds like you somehow got it working afterwards because you accepted an answer.

サインインしてコメントする。

カテゴリ

Help Center および File ExchangeLogical についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by