How to specific random numbers rang?
조회 수: 9 (최근 30일)
이전 댓글 표시
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 ^^
댓글 수: 0
채택된 답변
추가 답변 (4개)
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
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.
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.
댓글 수: 0
참고 항목
카테고리
Help Center 및 File Exchange에서 Creating and Concatenating Matrices에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!