How to specific random numbers rang?

조회 수: 9 (최근 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 CenterFile Exchange에서 Creating and Concatenating Matrices에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by