Discover MakerZone

MATLAB and Simulink resources for Arduino, LEGO, and Raspberry Pi

Learn more

Discover what MATLAB® can do for your career.

Opportunities for recent engineering grads.

Apply Today

Thread Subject:
weighted random number

Subject: weighted random number

From: samira

Date: 14 Jul, 2011 16:48:13

Message: 1 of 4

Hi all

I know how to make a random vector of a specified size from a set of numbers. Now can you help me how to make a weighted random vector.
I mean I have this vector [1 2 3], now I want to make another vector of size "n" which randomly includes these numbers. The point is I want to specify that in my random vector, I want 20% of the numbers be 1, 40% be 2 and next 40% be 3.

Is it possible?

Thanks

Subject: weighted random number

From: Jomar Bueyes

Date: 14 Jul, 2011 17:08:19

Message: 2 of 4

On Jul 14, 12:48 pm, "samira " <samira.rash...@gmail.com> wrote:
> Hi all
>
> I know how to make a random vector of a specified size from a set of numbers. Now can you help me how to make a weighted random vector.
> I mean I have this vector [1 2 3], now I want to make another vector of size "n" which randomly includes these numbers. The point is I want to specify that in my random vector, I want 20% of the numbers be 1, 40% be 2 and next 40% be 3.
>
> Is it possible?
>
> Thanks

Hi Samira,

It is possible but the solution depends on whether your random vector
must have on average 20% No 1, 40% No. 2, and 40% No. 3 or the random
vector must have exactly 20% No. 2, 40% No. 2, and 40% No. 3. In the
first case (percents are averages) this might work

t = rand(1, n); % t has random numbers in (0, 1)
v(t<=0.2) = 1; % On average 20% of the numbers will be in (0, 0.2]
v(0.2 < t & t <= 0.6) = 2; % On average 40% of the numbers will be in
(0.2, 0.6]
v(t > 0.6) = 1; % On average 40% of the numbers will be in (0.6, 1)

In the second case (percents are the exact number of 1s, 2s and 3s)
this might work

v = [ones(1, round(0.2*n)), 2*ones(1, 0.4*n), 3*ones(1,
round(0.4*n) )];

Thus far v has the correct percentages but is not random. To make it
random

v = v(randperm(n))

HTH,

Jomar

Subject: weighted random number

From: Doug Schwarz

Date: 14 Jul, 2011 17:17:17

Message: 3 of 4

On 7/14/2011 12:48 PM, samira wrote:
> Hi all
>
> I know how to make a random vector of a specified size from a set of
> numbers. Now can you help me how to make a weighted random vector.
> I mean I have this vector [1 2 3], now I want to make another vector of
> size "n" which randomly includes these numbers. The point is I want to
> specify that in my random vector, I want 20% of the numbers be 1, 40% be
> 2 and next 40% be 3.
>
> Is it possible?
>
> Thanks


% probabilities
p = [0.2 0.4 0.4];

% cumulative probabilities
cp = cumsum(p);

% random vector, k, with P[k==1] = p(1), P[k==2] = p(2), etc.
n = 100000;
[~,k] = histc(cp(end)*rand(n,1),[0,cp]);


--
Doug Schwarz
dmschwarz&ieee,org
Make obvious changes to get real email address.

Subject: weighted random number

From: ImageAnalyst

Date: 15 Jul, 2011 01:52:30

Message: 4 of 4

Nice example. And if you can follow it, here is a more general
discussion:
http://en.wikipedia.org/wiki/Inverse_transform_sampling
where you can specify a probability density function and get numbers
plucked from that distribution.

Tags for this Thread

What are tags?

A tag is like a keyword or category label associated with each thread. Tags make it easier for you to find threads of interest.

Anyone can tag a thread. Tags are public and visible to everyone.

Contact us