How to specific random numbers rang?

9 Ansichten (letzte 30 Tage)
Chanon
Chanon am 1 Mär. 2011
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 ^^

Akzeptierte Antwort

Paulo Silva
Paulo Silva am 1 Mär. 2011
randi([1 9],m,n)

Weitere Antworten (4)

Matt Fig
Matt Fig am 1 Mär. 2011
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 Kommentare
Walter Roberson
Walter Roberson am 1 Mär. 2011
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 am 1 Mär. 2011
Good enough for government work...;-)

Melden Sie sich an, um zu kommentieren.


Chanon
Chanon am 1 Mär. 2011
r=mu+sd.*randn([1 9],m,n);
T-T
warning : input argument must be scalar
  1 Kommentar
Walter Roberson
Walter Roberson am 1 Mär. 2011
You did not use randi() like Paulo suggested.

Melden Sie sich an, um zu kommentieren.


Walter Roberson
Walter Roberson am 1 Mär. 2011
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 am 1 Mär. 2011
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, ^_^

Kategorien

Mehr zu Creating and Concatenating Matrices finden Sie in Help Center und File Exchange

Tags

Community Treasure Hunt

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

Start Hunting!

Translated by