Selecting unique random numbers between 0 to 1 from a vector.

Hello,
I am trying to generate the unique random numbers between 0 to 1 from a obtained vector. I am using "randsample" function to generated the required number of random numbers from the vector 'y'. The thing is this vector 'y' is a cumulative distribution function (CDF) obtained from the manual sum of different probability distribution functions (PDF's). And it contains the large number of 0's and 0.75's.. and these are the two numbers which repeat themselves when I try to generate the random numbers using the "randsample". I am running this program for multiple (thousands) number of times. But at least in a single go, I need unique numbers.
can you suggest the way to generate the unique random numbers in this matter.
pos=randsample(y,4);

5 Comments

I found the solution to my problem though it is not this one exactly. My problem lied with the corresponding X - coordinates of the random number generated.
My steps were,
  1. To generate random numbers between 0 to 1 from my CDF.
  2. To find the corresponding location of these random numbers on X-axis from CDF.
As we know, in CDF, many data points on X axis can have the same cumulative probability like 1 or 0. So when I recall the corresponding position of 0.75 (cum. probability) on the X- axis, I get multiple numbers. And those created a problem.
Do you mean:
pos = randsample(unique(y), 4);
This would work actually. But as I mentioned in my earlier comment the problem lies in the positions of the extracted random numbers. These positions (on X- axis) are too many for a estimated random number. e.g. if the randsample produces 0.75 as random numebr then there are around 30-40 positions on X-axis which correspond to this value.
I do not understand, what the problem is. Maybe you mean:
index = randperm(1:numel(y), 4);
pickedX = x(index);
pickedY = y(index);
Thank you very much @Jan. The command unique helped.

Sign in to comment.

Answers (0)

Categories

Find more on Random Number Generation in Help Center and File Exchange

Products

Release

R2021a

Asked:

on 30 Aug 2021

Community Treasure Hunt

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

Start Hunting!