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 generator

Subject: Weighted random number generator

From: stinkinrich88 Coombez

Date: 22 Jul, 2008 09:00:09

Message: 1 of 20

Hello,

I would like a function similar to the rnd function but I
want the generated numbers to be weighted, i.e. I would like
the function to be more likely to give me a higher number
than a lower number and I would like to be able to specify
this probability.

For example, the betarnd function almost does what I want. I
can make a sloping probability distribution with it. The
only trouble is the probability distribution bulges slightly
in the middle and I want it to be straight.

Is there any function I can use to do this? Or a function
that gives me random numbers (between 0-1) using a formula
that I specify?

Thanks!

Subject: Weighted random number generator

From: Bruno Luong

Date: 22 Jul, 2008 09:21:03

Message: 2 of 20

"stinkinrich88 Coombez" <stinkinrich88@gmail.com> wrote in
message <g647ip$2d5$1@fred.mathworks.com>...

>
> Is there any function I can use to do this? Or a function
> that gives me random numbers (between 0-1) using a formula
> that I specify?
>

If you want to generate random variable X having pdf 'f'
(f>=0, integral f = 1), then do the following:

X = g(rand)

where g is the inverse of the cumulative integral of f, i.e.,

g(y) is a number such that
y = integral on [-inf,g(y)] f(t) dt

Bruno

Subject: Weighted random number generator

From: NZTideMan

Date: 22 Jul, 2008 09:29:57

Message: 3 of 20

On Jul 22, 9:00=A0pm, "stinkinrich88 Coombez" <stinkinric...@gmail.com>
wrote:
> Hello,
>
> I would like a function similar to the rnd function but I
> want the generated numbers to be weighted, i.e. I would like
> the function to be more likely to give me a higher number
> than a lower number and I would like to be able to specify
> this probability.
>
> For example, the betarnd function almost does what I want. I
> can make a sloping probability distribution with it. The
> only trouble is the probability distribution bulges slightly
> in the middle and I want it to be straight.
>
> Is there any function I can use to do this? Or a function
> that gives me random numbers (between 0-1) using a formula
> that I specify?
>
> Thanks!

Sounds like you know what shape the PDF should be. Why not sketch it,
then integrate to CDF and generate your random numbers using
bootstrapping? Search for empirical CDF in this newsgroup to see how.

Subject: Weighted random number generator

From: John D'Errico

Date: 22 Jul, 2008 09:53:01

Message: 4 of 20

"stinkinrich88 Coombez" <stinkinrich88@gmail.com> wrote in message
<g647ip$2d5$1@fred.mathworks.com>...
> Hello,
>
> I would like a function similar to the rnd function but I
> want the generated numbers to be weighted, i.e. I would like
> the function to be more likely to give me a higher number
> than a lower number and I would like to be able to specify
> this probability.
>
> For example, the betarnd function almost does what I want. I
> can make a sloping probability distribution with it. The
> only trouble is the probability distribution bulges slightly
> in the middle and I want it to be straight.
>
> Is there any function I can use to do this? Or a function
> that gives me random numbers (between 0-1) using a formula
> that I specify?
>
> Thanks!

While you can indeed generate your own
random generator for a linear pdf, it is
also trivial to use the stats toolbox and the
beta distribution. You just need to choose
the proper set of parameters.

ezplot(@(x) betapdf(x,2,1),[0 1])

John

Subject: Weighted random number generator

From: stinkinrich88 Coombez

Date: 22 Jul, 2008 11:12:03

Message: 5 of 20

"John D'Errico" <woodchips@rochester.rr.com> wrote in
message <g64alt$1kr$1@fred.mathworks.com>...
> "stinkinrich88 Coombez" <stinkinrich88@gmail.com> wrote in
message
> <g647ip$2d5$1@fred.mathworks.com>...
> > Hello,
> >
> > I would like a function similar to the rnd function but I
> > want the generated numbers to be weighted, i.e. I would like
> > the function to be more likely to give me a higher number
> > than a lower number and I would like to be able to specify
> > this probability.
> >
> > For example, the betarnd function almost does what I want. I
> > can make a sloping probability distribution with it. The
> > only trouble is the probability distribution bulges slightly
> > in the middle and I want it to be straight.
> >
> > Is there any function I can use to do this? Or a function
> > that gives me random numbers (between 0-1) using a formula
> > that I specify?
> >
> > Thanks!
>
> While you can indeed generate your own
> random generator for a linear pdf, it is
> also trivial to use the stats toolbox and the
> beta distribution. You just need to choose
> the proper set of parameters.
>
> ezplot(@(x) betapdf(x,2,1),[0 1])
>
> John

Thanks for your responses, everyone. I'm just getting my
head around them!

John, your solution looks great, the only trouble is I need
to be able to adjust the steepness of the slope. I have
tried adjusting the parameters for betapdf but I end up with
a curve. Is there a way I can adjust the steepness of the
slop while keeping it flat?

thanks!

Subject: Weighted random number generator

From: vedenev

Date: 22 Jul, 2008 11:28:14

Message: 6 of 20

I had such task and solve it. Here is the code of generator of random
of weighted integer numbers:

function an=weighted_rand(w)
% weighted random number w - weights
% random number from row 1,2,...,length(w)
% if some weight =0 than corresponding number is imposible


vn=1:1:length(w);
wt=w;

wtn=[];
vnn=[];
for wtc=1:1:length(wt)
    if wt(wtc)~=0
        wtn=[wtn wt(wtc)];
        vnn=[vnn vn(wtc)];
    end
end

wt=wtn;
vn=vnn;

lwt=length(wt);
wtn=wt/sum(wt); % normalized weights
% position definition array:
pdat=0;
pda=zeros(1,(lwt+2)); % position definition array
pda(1)=pdat;
for pdac=1:lwt
pdat=pdat+wtn(pdac);
pda(pdac+1)=pdat;
end
pda(lwt+2)=rand; % add random number
[pdas pdasi]=sort(pda);
[tmp ind1]=max(pdasi);
an1=ind1-1;
an=vn(an1);




it use rand and divide interval [0;1] for several regions not same
length and check where is point from rand.


------------------------------------
Maxim Vedenev, Matlab freelancer
http://simulations.narod.ru/

Subject: Weighted random number generator

From: Adam

Date: 22 Jul, 2008 13:04:02

Message: 7 of 20

"stinkinrich88 Coombez" <stinkinrich88@gmail.com> wrote in
message <g64fa3$hc7$1@fred.mathworks.com>...

<SNIP>

> a curve. Is there a way I can adjust the steepness of the
> slop while keeping it flat?

Maybe I'm missing something. But if you want the pdf to be
a straight line... It has to pass through (0,0) and (1,x)
where x is determined by the number of bins. so how is the
slope going to change?

another way:

N = 1e5;
vRandom = rand(1, N);
vWeighted = vRandom.^0.5;

~Adam

Subject: Weighted random number generator

From: stinkinrich88 Coombez

Date: 22 Jul, 2008 13:41:03

Message: 8 of 20

vedenev <vedenev.maxim@gmail.com> wrote in message
<30c420a4-7732-4f30-8884-a11806635acc@w7g2000hsa.googlegroups.com>...
> I had such task and solve it. Here is the code of
generator of random
> of weighted integer numbers:
>
> function an=weighted_rand(w)
> % weighted random number w - weights
> % random number from row 1,2,...,length(w)
> % if some weight =0 than corresponding number is imposible
>
>
> vn=1:1:length(w);
> wt=w;
>
> wtn=[];
> vnn=[];
> for wtc=1:1:length(wt)
> if wt(wtc)~=0
> wtn=[wtn wt(wtc)];
> vnn=[vnn vn(wtc)];
> end
> end
>
> wt=wtn;
> vn=vnn;
>
> lwt=length(wt);
> wtn=wt/sum(wt); % normalized weights
> % position definition array:
> pdat=0;
> pda=zeros(1,(lwt+2)); % position definition array
> pda(1)=pdat;
> for pdac=1:lwt
> pdat=pdat+wtn(pdac);
> pda(pdac+1)=pdat;
> end
> pda(lwt+2)=rand; % add random number
> [pdas pdasi]=sort(pda);
> [tmp ind1]=max(pdasi);
> an1=ind1-1;
> an=vn(an1);
>
>
>
>
> it use rand and divide interval [0;1] for several regions
not same
> length and check where is point from rand.
>
>
> ------------------------------------
> Maxim Vedenev, Matlab freelancer
> http://simulations.narod.ru/

thanks for the code, but I really need a continuous
distribution that gives me values between 0-1.

Subject: Weighted random number generator

From: stinkinrich88 Coombez

Date: 22 Jul, 2008 13:45:05

Message: 9 of 20

"Adam " <not.real@email.com> wrote in message
<g64ls2$22d$1@fred.mathworks.com>...
> "stinkinrich88 Coombez" <stinkinrich88@gmail.com> wrote in
> message <g64fa3$hc7$1@fred.mathworks.com>...
>
> <SNIP>
>
> > a curve. Is there a way I can adjust the steepness of the
> > slop while keeping it flat?
>
> Maybe I'm missing something. But if you want the pdf to be
> a straight line... It has to pass through (0,0) and (1,x)
> where x is determined by the number of bins. so how is the
> slope going to change?
>
> another way:
>
> N = 1e5;
> vRandom = rand(1, N);
> vWeighted = vRandom.^0.5;
>
> ~Adam

Thanks Adam. I made a little script to put your solution
into a graph:

N = 100000;
vRandom = rand(1, N);
vWeighted = vRandom.^0.5;
catsize = 100;
cats = zeros(1, catsize + 1);

for index = 1 : N
cats(floor(1 + (vWeighted(index)*catsize))) = cats(1 +
(floor(vWeighted(index)*catsize))) + 1;
end

scatter(0:1/catsize:1, cats);



It looks great, but I just literally want to be able to tilt
that slope about the centre so the probability of getting a
0 goes up and the probability of getting a 1 goes down and
everything in between gets adjusted accordingly. I want it
to work so that I could tilt the slope right back to
horizontal, i.e. a uniform distribution.

I can't find a way to 'tilt' your solution so I still can't
use it.

Subject: Weighted random number generator

From: Adam

Date: 22 Jul, 2008 15:15:05

Message: 10 of 20

"stinkinrich88 Coombez" <stinkinrich88@gmail.com> wrote in
message <g64o91$3sh$1@fred.mathworks.com>...
> "Adam " <not.real@email.com> wrote in message
> <g64ls2$22d$1@fred.mathworks.com>...
> > "stinkinrich88 Coombez" <stinkinrich88@gmail.com> wrote in
> > message <g64fa3$hc7$1@fred.mathworks.com>...
> >
> > <SNIP>
> >
> > > a curve. Is there a way I can adjust the steepness of the
> > > slop while keeping it flat?
> >
> > Maybe I'm missing something. But if you want the pdf to be
> > a straight line... It has to pass through (0,0) and (1,x)
> > where x is determined by the number of bins. so how is the
> > slope going to change?
> >
> > another way:
> >
> > N = 1e5;
> > vRandom = rand(1, N);
> > vWeighted = vRandom.^0.5;
> >
> > ~Adam
>
> Thanks Adam. I made a little script to put your solution
> into a graph:
>
> N = 100000;
> vRandom = rand(1, N);
> vWeighted = vRandom.^0.5;
<snip: reinventing the wheel...

try this
hist(vWeighted, 0:0.01:1)

> It looks great, but I just literally want to be able to tilt
> that slope about the centre so the probability of getting a
> 0 goes up and the probability of getting a 1 goes down and
> everything in between gets adjusted accordingly. I want it
> to work so that I could tilt the slope right back to
> horizontal, i.e. a uniform distribution.

ok. You want your pdf to be a line. This line has
adjustable slope, m, and goes through point (0.5, 0.5)

y = m(x-0.5) + 0.5

integrate to get cdf

y = m/2*x^2 + 0.5*(1-m)*x

set equal to zero and solve for x

x = (0.5(m-1) + sqrt(0.25*(1-m)^2+m*y))/m

y is rand(1, N)
x has adjustable slopey distribution based on m in [-1 1]

test
N = 1e6;
y = rand(1, N);
m = 0.25;
x = (0.5*(m-1)+sqrt(.25*(1-m)^2+m*y))/m;
hist(x, 0:0.01:1)

Really hope this wasn't a homework
~Adam

Subject: Weighted random number generator

From: Peter Boettcher

Date: 22 Jul, 2008 15:24:32

Message: 11 of 20

"stinkinrich88 Coombez" <stinkinrich88@gmail.com> writes:

> "Adam " <not.real@email.com> wrote in message
> <g64ls2$22d$1@fred.mathworks.com>...
>> "stinkinrich88 Coombez" <stinkinrich88@gmail.com> wrote in
>> message <g64fa3$hc7$1@fred.mathworks.com>...
>>
>> <SNIP>
>>
>> > a curve. Is there a way I can adjust the steepness of the
>> > slop while keeping it flat?
>>
>> Maybe I'm missing something. But if you want the pdf to be
>> a straight line... It has to pass through (0,0) and (1,x)
>> where x is determined by the number of bins. so how is the
>> slope going to change?
>>
>> another way:
>>
>> N = 1e5;
>> vRandom = rand(1, N);
>> vWeighted = vRandom.^0.5;
>>
>> ~Adam
>
> Thanks Adam. I made a little script to put your solution
> into a graph:
>
> N = 100000;
> vRandom = rand(1, N);
> vWeighted = vRandom.^0.5;
> catsize = 100;
> cats = zeros(1, catsize + 1);
>
> for index = 1 : N
> cats(floor(1 + (vWeighted(index)*catsize))) = cats(1 +
> (floor(vWeighted(index)*catsize))) + 1;
> end
>
> scatter(0:1/catsize:1, cats);
>
>
>
> It looks great, but I just literally want to be able to tilt
> that slope about the centre so the probability of getting a
> 0 goes up and the probability of getting a 1 goes down and
> everything in between gets adjusted accordingly. I want it
> to work so that I could tilt the slope right back to
> horizontal, i.e. a uniform distribution.

If you map uniform random variables on [0,1] through the inverse CDF,
you get random samples on your specified distribution.

Since you want a linear PDF with variable slope (your "tilt"), your PDF
is f(x) = m*x + b. Your CDF is just the integral,

F(x) = m*x^2/2 + bx + c

The constants are important. Your PDF is 0 outside [0,1], so F(0) = 0
and F(1) = 1.

F(0) = c = 0

F(x) = m*x^2/2 + bx

F(1) = m*1/2 + b = 1

So now pick your "m" to define the "tilt", and calculate b from that.

b = 1 - m/2

With "b" you can now plot both your PDF and your CDF.

f(x) = m*x + 1 - m/2 ( 0 <= x <= 1)
F(x) = m*x^2/2 + (1-m/2)x ( 0 <= x <= 1)


Invert F(x), then evaluate that inverse for a set of uniform random
numbers between 0 and 1, and you'll get a sample set for your choice of
m.


-Peter

Subject: Weighted random number generator

From: stinkinrich88

Date: 22 Jul, 2008 15:42:01

Message: 12 of 20

"Adam " <not.real@email.com> wrote in message
<g64thp$h45$1@fred.mathworks.com>...
> "stinkinrich88 Coombez" <stinkinrich88@gmail.com> wrote in
> message <g64o91$3sh$1@fred.mathworks.com>...
> > "Adam " <not.real@email.com> wrote in message
> > <g64ls2$22d$1@fred.mathworks.com>...
> > > "stinkinrich88 Coombez" <stinkinrich88@gmail.com> wrote in
> > > message <g64fa3$hc7$1@fred.mathworks.com>...
> > >
> > > <SNIP>
> > >
> > > > a curve. Is there a way I can adjust the steepness
of the
> > > > slop while keeping it flat?
> > >
> > > Maybe I'm missing something. But if you want the pdf
to be
> > > a straight line... It has to pass through (0,0) and (1,x)
> > > where x is determined by the number of bins. so how
is the
> > > slope going to change?
> > >
> > > another way:
> > >
> > > N = 1e5;
> > > vRandom = rand(1, N);
> > > vWeighted = vRandom.^0.5;
> > >
> > > ~Adam
> >
> > Thanks Adam. I made a little script to put your solution
> > into a graph:
> >
> > N = 100000;
> > vRandom = rand(1, N);
> > vWeighted = vRandom.^0.5;
> <snip: reinventing the wheel...
>
> try this
> hist(vWeighted, 0:0.01:1)
>
> > It looks great, but I just literally want to be able to tilt
> > that slope about the centre so the probability of getting a
> > 0 goes up and the probability of getting a 1 goes down and
> > everything in between gets adjusted accordingly. I want it
> > to work so that I could tilt the slope right back to
> > horizontal, i.e. a uniform distribution.
>
> ok. You want your pdf to be a line. This line has
> adjustable slope, m, and goes through point (0.5, 0.5)
>
> y = m(x-0.5) + 0.5
>
> integrate to get cdf
>
> y = m/2*x^2 + 0.5*(1-m)*x
>
> set equal to zero and solve for x
>
> x = (0.5(m-1) + sqrt(0.25*(1-m)^2+m*y))/m
>
> y is rand(1, N)
> x has adjustable slopey distribution based on m in [-1 1]
>
> test
> N = 1e6;
> y = rand(1, N);
> m = 0.25;
> x = (0.5*(m-1)+sqrt(.25*(1-m)^2+m*y))/m;
> hist(x, 0:0.01:1)
>
> Really hope this wasn't a homework
> ~Adam


Ahh!! Thanks so much, Adam! What a legend. I promise I'm
going to work really hard, now. I'm going to do all the
maths stuff myself from now on. I only started using matlab
last week so I feel a bit out of my depth, but I'm getting
there.

No, not homework! I'm making an opportunistic data network
simulator for a studentship! This random number generator is
going to be used to solve the problem with the Random
Waypoint Mobility Model where the average speed of the nodes
tends to minSpeed.

Thanks again! Problem solved!

Subject: Weighted random number generator

From: stinkinrich88

Date: 22 Jul, 2008 15:55:06

Message: 13 of 20

Peter Boettcher <boettcher@ll.mit.edu> wrote in message
<muy7ibe6ixb.fsf@G99-Boettcher.llan.ll.mit.edu>...
> "stinkinrich88 Coombez" <stinkinrich88@gmail.com> writes:
>
> > "Adam " <not.real@email.com> wrote in message
> > <g64ls2$22d$1@fred.mathworks.com>...
> >> "stinkinrich88 Coombez" <stinkinrich88@gmail.com> wrote in
> >> message <g64fa3$hc7$1@fred.mathworks.com>...
> >>
> >> <SNIP>
> >>
> >> > a curve. Is there a way I can adjust the steepness of the
> >> > slop while keeping it flat?
> >>
> >> Maybe I'm missing something. But if you want the pdf to be
> >> a straight line... It has to pass through (0,0) and (1,x)
> >> where x is determined by the number of bins. so how is the
> >> slope going to change?
> >>
> >> another way:
> >>
> >> N = 1e5;
> >> vRandom = rand(1, N);
> >> vWeighted = vRandom.^0.5;
> >>
> >> ~Adam
> >
> > Thanks Adam. I made a little script to put your solution
> > into a graph:
> >
> > N = 100000;
> > vRandom = rand(1, N);
> > vWeighted = vRandom.^0.5;
> > catsize = 100;
> > cats = zeros(1, catsize + 1);
> >
> > for index = 1 : N
> > cats(floor(1 + (vWeighted(index)*catsize))) = cats(1 +
> > (floor(vWeighted(index)*catsize))) + 1;
> > end
> >
> > scatter(0:1/catsize:1, cats);
> >
> >
> >
> > It looks great, but I just literally want to be able to tilt
> > that slope about the centre so the probability of getting a
> > 0 goes up and the probability of getting a 1 goes down and
> > everything in between gets adjusted accordingly. I want it
> > to work so that I could tilt the slope right back to
> > horizontal, i.e. a uniform distribution.
>
> If you map uniform random variables on [0,1] through the
inverse CDF,
> you get random samples on your specified distribution.
>
> Since you want a linear PDF with variable slope (your
"tilt"), your PDF
> is f(x) = m*x + b. Your CDF is just the integral,
>
> F(x) = m*x^2/2 + bx + c
>
> The constants are important. Your PDF is 0 outside [0,1],
so F(0) = 0
> and F(1) = 1.
>
> F(0) = c = 0
>
> F(x) = m*x^2/2 + bx
>
> F(1) = m*1/2 + b = 1
>
> So now pick your "m" to define the "tilt", and calculate b
from that.
>
> b = 1 - m/2
>
> With "b" you can now plot both your PDF and your CDF.
>
> f(x) = m*x + 1 - m/2 ( 0 <= x <= 1)
> F(x) = m*x^2/2 + (1-m/2)x ( 0 <= x <= 1)
>
>
> Invert F(x), then evaluate that inverse for a set of
uniform random
> numbers between 0 and 1, and you'll get a sample set for
your choice of
> m.
>
>
> -Peter

Thanks for your reply, Peter. I'm going to use

(0.5*(m-1)+sqrt(.25*(1-m)^2+m*rand))/m

as my solution because I find it simpler to understand.
Thanks again!

Subject: Weighted random number generator

From: Roger Stafford

Date: 22 Jul, 2008 16:17:02

Message: 14 of 20

"stinkinrich88 Coombez" <stinkinrich88@gmail.com> wrote in message
<g64o91$3sh$1@fred.mathworks.com>...
> .......
> It looks great, but I just literally want to be able to tilt
> that slope about the centre so the probability of getting a
> 0 goes up and the probability of getting a 1 goes down and
> everything in between gets adjusted accordingly. I want it
> to work so that I could tilt the slope right back to
> horizontal, i.e. a uniform distribution.
>
> I can't find a way to 'tilt' your solution so I still can't
> use it.

  I might as well join the crowd and take a crack at this. Let the linear pdf
function f(x) be 1-k at x = 0 and 1+k at x = 1, where we assume

 -1 <= k <= 1.

Then we must have

 f(x) = 2*k*x + 1 - k

The cdf, F(x), will be

 F(x) = k*x^2 + (1-k)*x

The inverse of F(x) is

 Finv(y) = 2*y/(sqrt((1-k)^2+4*k*y)+1-k)

Hence, to generate random variables with this distribution, use

 y = rand(m,n);
 x = 2*y./(sqrt((1-k)^2+4*k*y)+1-k);

Roger Stafford

Subject: Weighted random number generator

From: Roger Stafford

Date: 22 Jul, 2008 16:56:02

Message: 15 of 20

"Roger Stafford" <ellieandrogerxyzzy@mindspring.com.invalid> wrote in
message <g6515u$5ou$1@fred.mathworks.com>...
> ........
> Hence, to generate random variables with this distribution, use
>
> y = rand(m,n);
> x = 2*y./(sqrt((1-k)^2+4*k*y)+1-k);
>
> Roger Stafford

  I should add that the formula

x = 2*y./(sqrt((1-k)^2+4*k*y)+1-k)

continues to be valid even if k is zero, that is, even if the desired slope is zero.

Roger Stafford

Subject: Weighted random number generator

From: stinkinrich88

Date: 22 Jul, 2008 17:24:02

Message: 16 of 20

"Roger Stafford" <ellieandrogerxyzzy@mindspring.com.invalid>
wrote in message <g653f2$slp$1@fred.mathworks.com>...
> "Roger Stafford"
<ellieandrogerxyzzy@mindspring.com.invalid> wrote in
> message <g6515u$5ou$1@fred.mathworks.com>...
> > ........
> > Hence, to generate random variables with this
distribution, use
> >
> > y = rand(m,n);
> > x = 2*y./(sqrt((1-k)^2+4*k*y)+1-k);
> >
> > Roger Stafford
>
> I should add that the formula
>
> x = 2*y./(sqrt((1-k)^2+4*k*y)+1-k)
>
> continues to be valid even if k is zero, that is, even if
the desired slope is zero.
>
> Roger Stafford
>

No way! Roger, that's brilliant! thanks! I was kinda bummed
that I couldn't have a horizontal slope with my old solution
so this one has made me even happier.

Thanks everyone!!

Subject: Weighted random number generator

From: John D'Errico

Date: 22 Jul, 2008 18:10:19

Message: 17 of 20

"stinkinrich88 Coombez" <stinkinrich88@gmail.com> wrote in message
<g64fa3$hc7$1@fred.mathworks.com>...
> "John D'Errico" <woodchips@rochester.rr.com> wrote in
> message <g64alt$1kr$1@fred.mathworks.com>...
> > "stinkinrich88 Coombez" <stinkinrich88@gmail.com> wrote in
> message
> > <g647ip$2d5$1@fred.mathworks.com>...
> > > Hello,
> > >
> > > I would like a function similar to the rnd function but I
> > > want the generated numbers to be weighted, i.e. I would like
> > > the function to be more likely to give me a higher number
> > > than a lower number and I would like to be able to specify
> > > this probability.
> > >
> > > For example, the betarnd function almost does what I want. I
> > > can make a sloping probability distribution with it. The
> > > only trouble is the probability distribution bulges slightly
> > > in the middle and I want it to be straight.
> > >
> > > Is there any function I can use to do this? Or a function
> > > that gives me random numbers (between 0-1) using a formula
> > > that I specify?
> > >
> > > Thanks!
> >
> > While you can indeed generate your own
> > random generator for a linear pdf, it is
> > also trivial to use the stats toolbox and the
> > beta distribution. You just need to choose
> > the proper set of parameters.
> >
> > ezplot(@(x) betapdf(x,2,1),[0 1])
> >
> > John
>
> Thanks for your responses, everyone. I'm just getting my
> head around them!
>
> John, your solution looks great, the only trouble is I need
> to be able to adjust the steepness of the slope. I have
> tried adjusting the parameters for betapdf but I end up with
> a curve. Is there a way I can adjust the steepness of the
> slop while keeping it flat?


A pdf cannot have its slope "adjusted".

That is defined by the requirement that
your pdf MUST integrate to 1.

You said you wanted a linear PDF. Do you
intend that p(0) not be zero? This is the
only degree of freedom that is available.

Assume that your pdf is defined over [0,1].

  pdf(x) = a + b*x

So we will define the value of the pdf at
x = 0 as a. Remember that the integral
of this function must be 1. So if we
integrate this function

  a*x + b*x^2/2

we come to the conclusion that

  a + b/2 = 1

So the slope has only one possible value.

  b = 2*(1 - a)

The value of the right end point of the pdf
must also be non-negative.

  pdf(x) = a + 2*(1-a)*x

  pdf(1) = a + 2 - 2*a = 2 - a

So we come to the conclusion that a linear
pdf over the interval [0,1] must have its
constant term (a) in the interval

   0 <= a <= 2

The cdf is the integral. So we have

  cdf(x) = a*x + (1-a)*x^2

All possible linear pdfs with support on
[0,1] must come from this family of pdfs.
Now if you want to generate a random
number from this distribution, just form
the inverse. Generate a random number
using rand.

  r = rand();
  
Pick your favorite value for a. I'll use
a = 1.5, and now solve for x.

  (1-a)*x^2 + a*x - r = 0

Lets do this for a large sample, then plot
the histogram.

  a = 1.5;
  r = rand(1000000,1);
  x = (-a + sqrt(a.^2 + 4*(1-a)*r))./(2*(1-a));
  hist(x,500)

HTH,
John

Subject: Weighted random number generator

From: Tim

Date: 26 Mar, 2011 01:14:04

Message: 18 of 20

I'm trying to do something very similar and I'm still confused on how to do it. Maybe what I'm doing is a bit different. Normally the random number generator has an equal probability of producing a number between 1 and zero. I would like to have the probability distribution fit the cosine squared function, with a shift. So the probability of getting a zero is the highest and the probability of getting pi/2 or -pi/2 is zero. Does that make sense?

Subject: Weighted random number generator

From: stinkinrich88

Date: 26 Mar, 2011 10:27:02

Message: 19 of 20

"Tim " <junk@gmail.com> wrote in message <imjeks$hc8$1@fred.mathworks.com>...
> I'm trying to do something very similar and I'm still confused on how to do it. Maybe what I'm doing is a bit different. Normally the random number generator has an equal probability of producing a number between 1 and zero. I would like to have the probability distribution fit the cosine squared function, with a shift. So the probability of getting a zero is the highest and the probability of getting pi/2 or -pi/2 is zero. Does that make sense?

Hi Tim,

I don't really know how to do this, but look at the help page called "Random Number Generation" in the "Probability Distributions" section of your Matlab help pages (I can't find it online). Especially look at the Acceptance-Rejection section on that page. Hope this helps you!

Subject: Weighted random number generator

From: Roger Stafford

Date: 29 Mar, 2011 03:14:03

Message: 20 of 20

"Tim " <junk@gmail.com> wrote in message <imjeks$hc8$1@fred.mathworks.com>...
> I'm trying to do something very similar and I'm still confused on how to do it. Maybe what I'm doing is a bit different. Normally the random number generator has an equal probability of producing a number between 1 and zero. I would like to have the probability distribution fit the cosine squared function, with a shift. So the probability of getting a zero is the highest and the probability of getting pi/2 or -pi/2 is zero. Does that make sense?
- - - - - - - - - -
  The identity cos(x)^2 = (1+cos(2*x))/2 implies that your distribution is the "raised cosine distribution". See

 http://en.wikipedia.org/wiki/Raised_cosine_distribution

  You can generate it without rejection using matlab's 'rand' function as follows:

 n = 1000; % Specify the number of random values to be generated
 r = 2*pi*(rand(n,1)-1/2);
 r = sign(r).*abs(r).^(1/3);
 x = 1.7748*r+0.1726*r.^3; % Initial inverse estimate
 for k = 1:4 % Four iterations will suffice to make inverse precise
  dif = sign(x).*(abs(x)-sin(abs(x))).^(1/3) - r; % Amount of error
  der = 0.550321-0.0275161*x.^2+0.000327572*x.^4; % Approx. derivative
  x = x - dif./der; % Make Gauss correction
 end
 x = x+pi*(1-2*(x>=0)); % Swap halves

  These x values will range between -pi and +pi. In your case you should divide x by two to produce a distribution between -pi/2 and +pi/2. For the general raised cosine distribution, an appropriate shift and scale factor change on x would be needed.

Roger Stafford

Tags for this Thread

No tags are associated with 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