control the threshold of random numbers
4 vues (au cours des 30 derniers jours)
Afficher commentaires plus anciens
abdul rehman
le 1 Juil 2021
Modifié(e) : John D'Errico
le 1 Juil 2021
I want to control the threshold of random numbers. In following code 1st column I want more on higer side e.g more (0.5 to 0.9 and less 0.1 to 0.5). In second column I want more 1s than 0s.
code:
Carr_veh = [rand(100, 1), randi([0, 1], 100, 1 )]
0 commentaires
Réponse acceptée
John D'Errico
le 1 Juil 2021
Modifié(e) : John D'Errico
le 1 Juil 2021
You just say you want a distribution of numbers that is not uniform. But if you say only what it is not, that is not enough to say what it is. If I tell you that my car is not red, do you know the color?
If you have the stats toolbox, then you will most easily use a beta distribution to generate samples. A beta gives you a lot of control over the shape. For example, we can get a roughly triangular distribution of numbers using 2 and 1 as beta parameters.
X = betarnd(2,1,[10000,1]);
hist(X,100)
But you can slso get what you may want using a simple transformation of rand, if you don't have the stats toolbox.
X = rand(10000,1).^(0.4);
hist(X,100)
By controlling the power you raise those numbers to, you can have a fiar amount of control. If the power is 0.5, again you will get a triangular distrbution.
You can generate more ones than zeros using a discrete random number generator. But you can do that as eaily using rand. For example, if you want 70% ones, this will do it:
Y = rand(10000,1) < 0.7;
hist(Y)
As you see, we have 70% ones in the vector.
It is up to you to decide what you want though, and that will help you choose a random number tool for your purposes,
0 commentaires
Plus de réponses (1)
Yongjian Feng
le 1 Juil 2021
Hello abdul,
Both rand and randi generate uniformly distributed pseudorandom numbers. Now you want something not uniformly distributed, but you didn't specify what kind of distribution you are looking for.
One possible way to do it is to scale the generated random number. For example, instead of randi([0,1], 100, 1]), use randi([0,2], 100, 1). Then add an additional logic
if res == 2
res = 1;
end
By doing so, there are twice 1s than 0s.
Similar idea for rand(100, 1). You can scale (0, 0.3) to (0, 0.5) and (0.3, 1) to (0.5, 1). By doing so, you have 30% (0, 0.5) and 70% (0.5, 1)
Thanks,
Yongjian
0 commentaires
Voir également
Catégories
En savoir plus sur Random Number Generation 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!