control the threshold of random numbers
3 views (last 30 days)
Show older comments
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 Comments
Accepted Answer
John D'Errico
on 1 Jul 2021
Edited: John D'Errico
on 1 Jul 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 Comments
More Answers (1)
Yongjian Feng
on 1 Jul 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 Comments
See Also
Categories
Find more on Random Number Generation in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!