round random a number

6 ビュー (過去 30 日間)
Vadim Tambovtsev
Vadim Tambovtsev 2016 年 10 月 26 日
編集済み: Bruno Luong 2018 年 9 月 14 日
Hello,
Suppose we have a number "3.76" and we want to round it either to 3 or 4 in a random way, how to do it? I didn't find the proper command in Matlab.

採用された回答

James Tursa
James Tursa 2016 年 10 月 26 日
編集済み: James Tursa 2016 年 10 月 26 日
For purely random rounding regardless of the fractional part:
x = your number
result = round(floor(x)+rand);
  1 件のコメント
Bruno Luong
Bruno Luong 2018 年 9 月 13 日
編集済み: Bruno Luong 2018 年 9 月 14 日
James's solution gives equi-probability between 3 and 4. If one want to simulate some sort of quantification noise, might be it is desired to have larger probability of 4 than 3 for 3.76 as input, something like
round(x+rand-0.5)
or (almost) equivalently
floor(x + rand)

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

その他の回答 (1 件)

Layan Alshuhumi
Layan Alshuhumi 2018 年 9 月 13 日
Make two variables, “Nmax” and “Nmin” and set Nmax to 5 and Nmin to 3 using the first two lines. On the third line, write the code to generate a random floating point number between 3 and 5 referencing your predefined variable

カテゴリ

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