control the threshold of random numbers
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 )]
Accepted Answer
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
Categories
Find more on Univariate Discrete Distributions 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!

