How to specific random numbers rang?

10 ビュー (過去 30 日間)
Chanon
Chanon 2011 年 3 月 1 日
Hello.
This syntax gave me a specific mean (mu) and specific standard diviation but how to specific range of random numbers? such as between 1 to 9,like 5 2 3 4 1 5 6 7 7 9 but not 1.2 or 5.5
r=mu+sd.*randn(m,n);
all I need is a set of random numbers with the same mu, SD and range between 1 to 9.
Thankyou for your kineness and so sorry about my broken english ^^

採用された回答

Paulo Silva
Paulo Silva 2011 年 3 月 1 日
randi([1 9],m,n)

その他の回答 (4 件)

Matt Fig
Matt Fig 2011 年 3 月 1 日
As Walter stated, this is not really possible exactly. However, you can get close (to some level of tolerance):
x = ceil(4.5 + .925.*randn(10000,1)); % The distribution
[min(x) max(x) mean(x)] % look at range.
hist(x,unique(x)); % Look at distribution.
  2 件のコメント
Walter Roberson
Walter Roberson 2011 年 3 月 1 日
A quick test to see how many iterations you go before generating something that would be out of range:
T = 0;while true; T = T + 1;x = ceil(4.5 + 0.925*randn); if x < 1 || x > 9; disp([T,x]); break; end; end
Over 11 runs the average number of iterations in my trial was 670586, so as an approximation, one value would be out of range for every 2/3 of a million generated points.
Matt Fig
Matt Fig 2011 年 3 月 1 日
Good enough for government work...;-)

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


Chanon
Chanon 2011 年 3 月 1 日
r=mu+sd.*randn([1 9],m,n);
T-T
warning : input argument must be scalar
  1 件のコメント
Walter Roberson
Walter Roberson 2011 年 3 月 1 日
You did not use randi() like Paulo suggested.

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


Walter Roberson
Walter Roberson 2011 年 3 月 1 日
It is not possible to constrain a normal distribution to a particular range. A normal distribution has an infinite tail in both directions. If you are required to select from a finite set of outcomes, then the distribution can never be a normal distribution (but it might be a good approximation if the number of different outcomes is large enough.)
Paulo's answer is fine if you want to select integers from 1 to 9 with equal probability (uniform distribution)
If you want to select 1 to 9, each with probabilities P(K) (i.e., P is a vector that gives the probabilities) then
[counts, bins] = histc(rand(1,n*m), [0 cumsum(P(1:end-1)) 1]; r = reshape(bins, n, m);
It is of course straight-forward to modify this to start at an integer other than 1.

Chanon
Chanon 2011 年 3 月 1 日
Million thanks for you guys, ^_^ I'm new for this MATLAB but for some reason I have to work with this also I'm an idiot for statistic too, this is so much help me solve the problem faster than reading the books, ^_^

カテゴリ

Help Center および File ExchangeCreating and Concatenating Matrices についてさらに検索

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by