How to specific random numbers rang?
10 vues (au cours des 30 derniers jours)
Afficher commentaires plus anciens
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 commentaires
Réponse acceptée
Plus de réponses (4)
Matt Fig
le 1 Mar 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 commentaires
Walter Roberson
le 1 Mar 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.
Walter Roberson
le 1 Mar 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.
0 commentaires
Voir également
Catégories
En savoir plus sur Creating and Concatenating Matrices dans Help Center et File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!