フィルターのクリア

How a generate random numbers within a probabilistic range?

1 回表示 (過去 30 日間)
Nikolas Spiliopoulos
Nikolas Spiliopoulos 2016 年 11 月 3 日
Hi there,
I would like to generate random numbers for an input of 48 variables within a probabilistic range. I managed to get results for a particular range (lets say -0.02 +0.02), using rand function. However how can I do the same for a probabilistic range like this:
50% probability the range to be -0.01 +0.01, 30% probability the range to be -0.02 +0.02 and so on.
many thanks
Nikolas
  2 件のコメント
Torsten
Torsten 2016 年 11 月 3 日
How can the probability become smaller for a wider range ?
Best wishes
Torsten.
Nikolas Spiliopoulos
Nikolas Spiliopoulos 2016 年 11 月 3 日
Hi again,
thanks for the prompt reply.
Well it has to do with the problem, it's more likely the values to change within a range of -0.01 to +0.01 rather than a range of -0.02 +0.02.
hope that helps
Nikolas

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

回答 (1 件)

Adam
Adam 2016 年 11 月 3 日
編集済み: Adam 2016 年 11 月 3 日
You could just do a simple 2 layer approach if rand gives you an acceptable distribution within each of your ranges.
e.g.
ranges = [-0.01 0.01; -0.02 0.02; -0.05 0.05];
rangeProbabilities = [0.5 0.3 0.2];
rangeVal = rand( )
rangeIdx = find( cumsum( rangeProbabilities ) >= rangeVal , 1 )
n = 48;
randomNumbers = ( rand( n, 1 ) -0.5 ) * 2 * ranges( rangeIdx, 2 ); % Produce n random numbers in range
Note that last line assumes your ranges are symmetric (in which case there's no point storing the 1st column anyway). If not then it is still simple enough to ensure a random number falls within a single predefined range.
  1 件のコメント
Nikolas Spiliopoulos
Nikolas Spiliopoulos 2016 年 11 月 3 日
ok, I"ll give it a try Adam,
thank you so much
regards
Nikolas

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

Community Treasure Hunt

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

Start Hunting!

Translated by