http://www.mathworks.com/matlabcentral/newsreader/view_thread/173000
MATLAB Central Newsreader  Weighted random number generator
Feed for thread: Weighted random number generator
enus
©19942015 by MathWorks, Inc.
webmaster@mathworks.com
MATLAB Central Newsreader
http://blogs.law.harvard.edu/tech/rss
60
MathWorks
http://www.mathworks.com/images/membrane_icon.gif

Tue, 22 Jul 2008 09:00:09 +0000
Weighted random number generator
http://www.mathworks.com/matlabcentral/newsreader/view_thread/173000#444500
stinkinrich88 Coombez
Hello,<br>
<br>
I would like a function similar to the rnd function but I<br>
want the generated numbers to be weighted, i.e. I would like<br>
the function to be more likely to give me a higher number<br>
than a lower number and I would like to be able to specify<br>
this probability. <br>
<br>
For example, the betarnd function almost does what I want. I<br>
can make a sloping probability distribution with it. The<br>
only trouble is the probability distribution bulges slightly<br>
in the middle and I want it to be straight. <br>
<br>
Is there any function I can use to do this? Or a function<br>
that gives me random numbers (between 01) using a formula<br>
that I specify?<br>
<br>
Thanks!

Tue, 22 Jul 2008 09:21:03 +0000
Re: Weighted random number generator
http://www.mathworks.com/matlabcentral/newsreader/view_thread/173000#444504
Bruno Luong
"stinkinrich88 Coombez" <stinkinrich88@gmail.com> wrote in<br>
message <g647ip$2d5$1@fred.mathworks.com>...<br>
<br>
> <br>
> Is there any function I can use to do this? Or a function<br>
> that gives me random numbers (between 01) using a formula<br>
> that I specify?<br>
> <br>
<br>
If you want to generate random variable X having pdf 'f'<br>
(f>=0, integral f = 1), then do the following:<br>
<br>
X = g(rand)<br>
<br>
where g is the inverse of the cumulative integral of f, i.e.,<br>
<br>
g(y) is a number such that<br>
y = integral on [inf,g(y)] f(t) dt<br>
<br>
Bruno

Tue, 22 Jul 2008 09:29:57 +0000
Re: Weighted random number generator
http://www.mathworks.com/matlabcentral/newsreader/view_thread/173000#444505
NZTideMan
On Jul 22, 9:00=A0pm, "stinkinrich88 Coombez" <stinkinric...@gmail.com><br>
wrote:<br>
> Hello,<br>
><br>
> I would like a function similar to the rnd function but I<br>
> want the generated numbers to be weighted, i.e. I would like<br>
> the function to be more likely to give me a higher number<br>
> than a lower number and I would like to be able to specify<br>
> this probability.<br>
><br>
> For example, the betarnd function almost does what I want. I<br>
> can make a sloping probability distribution with it. The<br>
> only trouble is the probability distribution bulges slightly<br>
> in the middle and I want it to be straight.<br>
><br>
> Is there any function I can use to do this? Or a function<br>
> that gives me random numbers (between 01) using a formula<br>
> that I specify?<br>
><br>
> Thanks!<br>
<br>
Sounds like you know what shape the PDF should be. Why not sketch it,<br>
then integrate to CDF and generate your random numbers using<br>
bootstrapping? Search for empirical CDF in this newsgroup to see how.

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

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

Tue, 22 Jul 2008 11:28:14 +0000
Re: Weighted random number generator
http://www.mathworks.com/matlabcentral/newsreader/view_thread/173000#444546
vedenev
I had such task and solve it. Here is the code of generator of random<br>
of weighted integer numbers:<br>
<br>
function an=weighted_rand(w)<br>
% weighted random number w  weights<br>
% random number from row 1,2,...,length(w)<br>
% if some weight =0 than corresponding number is imposible<br>
<br>
<br>
vn=1:1:length(w);<br>
wt=w;<br>
<br>
wtn=[];<br>
vnn=[];<br>
for wtc=1:1:length(wt)<br>
if wt(wtc)~=0<br>
wtn=[wtn wt(wtc)];<br>
vnn=[vnn vn(wtc)];<br>
end<br>
end<br>
<br>
wt=wtn;<br>
vn=vnn;<br>
<br>
lwt=length(wt);<br>
wtn=wt/sum(wt); % normalized weights<br>
% position definition array:<br>
pdat=0;<br>
pda=zeros(1,(lwt+2)); % position definition array<br>
pda(1)=pdat;<br>
for pdac=1:lwt<br>
pdat=pdat+wtn(pdac);<br>
pda(pdac+1)=pdat;<br>
end<br>
pda(lwt+2)=rand; % add random number<br>
[pdas pdasi]=sort(pda);<br>
[tmp ind1]=max(pdasi);<br>
an1=ind11;<br>
an=vn(an1);<br>
<br>
<br>
<br>
<br>
it use rand and divide interval [0;1] for several regions not same<br>
length and check where is point from rand.<br>
<br>
<br>
<br>
Maxim Vedenev, Matlab freelancer<br>
<a href="http://simulations.narod.ru/">http://simulations.narod.ru/</a>

Tue, 22 Jul 2008 13:04:02 +0000
Re: Weighted random number generator
http://www.mathworks.com/matlabcentral/newsreader/view_thread/173000#444581
Adam
"stinkinrich88 Coombez" <stinkinrich88@gmail.com> wrote in<br>
message <g64fa3$hc7$1@fred.mathworks.com>...<br>
<br>
<SNIP><br>
<br>
> a curve. Is there a way I can adjust the steepness of the<br>
> slop while keeping it flat? <br>
<br>
Maybe I'm missing something. But if you want the pdf to be<br>
a straight line... It has to pass through (0,0) and (1,x)<br>
where x is determined by the number of bins. so how is the<br>
slope going to change?<br>
<br>
another way:<br>
<br>
N = 1e5;<br>
vRandom = rand(1, N);<br>
vWeighted = vRandom.^0.5;<br>
<br>
~Adam

Tue, 22 Jul 2008 13:41:03 +0000
Re: Weighted random number generator
http://www.mathworks.com/matlabcentral/newsreader/view_thread/173000#444596
stinkinrich88 Coombez
vedenev <vedenev.maxim@gmail.com> wrote in message<br>
<30c420a477324f308884a11806635acc@w7g2000hsa.googlegroups.com>...<br>
> I had such task and solve it. Here is the code of<br>
generator of random<br>
> of weighted integer numbers:<br>
> <br>
> function an=weighted_rand(w)<br>
> % weighted random number w  weights<br>
> % random number from row 1,2,...,length(w)<br>
> % if some weight =0 than corresponding number is imposible<br>
> <br>
> <br>
> vn=1:1:length(w);<br>
> wt=w;<br>
> <br>
> wtn=[];<br>
> vnn=[];<br>
> for wtc=1:1:length(wt)<br>
> if wt(wtc)~=0<br>
> wtn=[wtn wt(wtc)];<br>
> vnn=[vnn vn(wtc)];<br>
> end<br>
> end<br>
> <br>
> wt=wtn;<br>
> vn=vnn;<br>
> <br>
> lwt=length(wt);<br>
> wtn=wt/sum(wt); % normalized weights<br>
> % position definition array:<br>
> pdat=0;<br>
> pda=zeros(1,(lwt+2)); % position definition array<br>
> pda(1)=pdat;<br>
> for pdac=1:lwt<br>
> pdat=pdat+wtn(pdac);<br>
> pda(pdac+1)=pdat;<br>
> end<br>
> pda(lwt+2)=rand; % add random number<br>
> [pdas pdasi]=sort(pda);<br>
> [tmp ind1]=max(pdasi);<br>
> an1=ind11;<br>
> an=vn(an1);<br>
> <br>
> <br>
> <br>
> <br>
> it use rand and divide interval [0;1] for several regions<br>
not same<br>
> length and check where is point from rand.<br>
> <br>
> <br>
> <br>
> Maxim Vedenev, Matlab freelancer<br>
> <a href="http://simulations.narod.ru/">http://simulations.narod.ru/</a><br>
<br>
thanks for the code, but I really need a continuous<br>
distribution that gives me values between 01.

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

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

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

Tue, 22 Jul 2008 15:42:01 +0000
Re: Weighted random number generator
http://www.mathworks.com/matlabcentral/newsreader/view_thread/173000#444638
stinkinrich88
"Adam " <not.real@email.com> wrote in message<br>
<g64thp$h45$1@fred.mathworks.com>...<br>
> "stinkinrich88 Coombez" <stinkinrich88@gmail.com> wrote in<br>
> message <g64o91$3sh$1@fred.mathworks.com>...<br>
> > "Adam " <not.real@email.com> wrote in message<br>
> > <g64ls2$22d$1@fred.mathworks.com>...<br>
> > > "stinkinrich88 Coombez" <stinkinrich88@gmail.com> wrote in<br>
> > > message <g64fa3$hc7$1@fred.mathworks.com>...<br>
> > > <br>
> > > <SNIP><br>
> > > <br>
> > > > a curve. Is there a way I can adjust the steepness<br>
of the<br>
> > > > slop while keeping it flat? <br>
> > > <br>
> > > Maybe I'm missing something. But if you want the pdf<br>
to be<br>
> > > a straight line... It has to pass through (0,0) and (1,x)<br>
> > > where x is determined by the number of bins. so how<br>
is the<br>
> > > slope going to change?<br>
> > > <br>
> > > another way:<br>
> > > <br>
> > > N = 1e5;<br>
> > > vRandom = rand(1, N);<br>
> > > vWeighted = vRandom.^0.5;<br>
> > > <br>
> > > ~Adam<br>
> > <br>
> > Thanks Adam. I made a little script to put your solution<br>
> > into a graph:<br>
> > <br>
> > N = 100000;<br>
> > vRandom = rand(1, N);<br>
> > vWeighted = vRandom.^0.5;<br>
> <snip: reinventing the wheel...<br>
> <br>
> try this<br>
> hist(vWeighted, 0:0.01:1)<br>
> <br>
> > It looks great, but I just literally want to be able to tilt<br>
> > that slope about the centre so the probability of getting a<br>
> > 0 goes up and the probability of getting a 1 goes down and<br>
> > everything in between gets adjusted accordingly. I want it<br>
> > to work so that I could tilt the slope right back to<br>
> > horizontal, i.e. a uniform distribution. <br>
> <br>
> ok. You want your pdf to be a line. This line has<br>
> adjustable slope, m, and goes through point (0.5, 0.5)<br>
> <br>
> y = m(x0.5) + 0.5<br>
> <br>
> integrate to get cdf<br>
> <br>
> y = m/2*x^2 + 0.5*(1m)*x<br>
> <br>
> set equal to zero and solve for x<br>
> <br>
> x = (0.5(m1) + sqrt(0.25*(1m)^2+m*y))/m<br>
> <br>
> y is rand(1, N)<br>
> x has adjustable slopey distribution based on m in [1 1]<br>
> <br>
> test<br>
> N = 1e6;<br>
> y = rand(1, N);<br>
> m = 0.25;<br>
> x = (0.5*(m1)+sqrt(.25*(1m)^2+m*y))/m;<br>
> hist(x, 0:0.01:1)<br>
> <br>
> Really hope this wasn't a homework<br>
> ~Adam<br>
<br>
<br>
Ahh!! Thanks so much, Adam! What a legend. I promise I'm<br>
going to work really hard, now. I'm going to do all the<br>
maths stuff myself from now on. I only started using matlab<br>
last week so I feel a bit out of my depth, but I'm getting<br>
there.<br>
<br>
No, not homework! I'm making an opportunistic data network<br>
simulator for a studentship! This random number generator is<br>
going to be used to solve the problem with the Random<br>
Waypoint Mobility Model where the average speed of the nodes<br>
tends to minSpeed. <br>
<br>
Thanks again! Problem solved!

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

Tue, 22 Jul 2008 16:17:02 +0000
Re: Weighted random number generator
http://www.mathworks.com/matlabcentral/newsreader/view_thread/173000#444655
Roger Stafford
"stinkinrich88 Coombez" <stinkinrich88@gmail.com> wrote in message <br>
<g64o91$3sh$1@fred.mathworks.com>...<br>
> .......<br>
> It looks great, but I just literally want to be able to tilt<br>
> that slope about the centre so the probability of getting a<br>
> 0 goes up and the probability of getting a 1 goes down and<br>
> everything in between gets adjusted accordingly. I want it<br>
> to work so that I could tilt the slope right back to<br>
> horizontal, i.e. a uniform distribution. <br>
> <br>
> I can't find a way to 'tilt' your solution so I still can't<br>
> use it. <br>
<br>
I might as well join the crowd and take a crack at this. Let the linear pdf <br>
function f(x) be 1k at x = 0 and 1+k at x = 1, where we assume<br>
<br>
1 <= k <= 1.<br>
<br>
Then we must have<br>
<br>
f(x) = 2*k*x + 1  k<br>
<br>
The cdf, F(x), will be<br>
<br>
F(x) = k*x^2 + (1k)*x<br>
<br>
The inverse of F(x) is <br>
<br>
Finv(y) = 2*y/(sqrt((1k)^2+4*k*y)+1k)<br>
<br>
Hence, to generate random variables with this distribution, use<br>
<br>
y = rand(m,n);<br>
x = 2*y./(sqrt((1k)^2+4*k*y)+1k);<br>
<br>
Roger Stafford

Tue, 22 Jul 2008 16:56:02 +0000
Re: Weighted random number generator
http://www.mathworks.com/matlabcentral/newsreader/view_thread/173000#444666
Roger Stafford
"Roger Stafford" <ellieandrogerxyzzy@mindspring.com.invalid> wrote in <br>
message <g6515u$5ou$1@fred.mathworks.com>...<br>
> ........<br>
> Hence, to generate random variables with this distribution, use<br>
> <br>
> y = rand(m,n);<br>
> x = 2*y./(sqrt((1k)^2+4*k*y)+1k);<br>
> <br>
> Roger Stafford<br>
<br>
I should add that the formula<br>
<br>
x = 2*y./(sqrt((1k)^2+4*k*y)+1k)<br>
<br>
continues to be valid even if k is zero, that is, even if the desired slope is zero.<br>
<br>
Roger Stafford

Tue, 22 Jul 2008 17:24:02 +0000
Re: Weighted random number generator
http://www.mathworks.com/matlabcentral/newsreader/view_thread/173000#444682
stinkinrich88
"Roger Stafford" <ellieandrogerxyzzy@mindspring.com.invalid><br>
wrote in message <g653f2$slp$1@fred.mathworks.com>...<br>
> "Roger Stafford"<br>
<ellieandrogerxyzzy@mindspring.com.invalid> wrote in <br>
> message <g6515u$5ou$1@fred.mathworks.com>...<br>
> > ........<br>
> > Hence, to generate random variables with this<br>
distribution, use<br>
> > <br>
> > y = rand(m,n);<br>
> > x = 2*y./(sqrt((1k)^2+4*k*y)+1k);<br>
> > <br>
> > Roger Stafford<br>
> <br>
> I should add that the formula<br>
> <br>
> x = 2*y./(sqrt((1k)^2+4*k*y)+1k)<br>
> <br>
> continues to be valid even if k is zero, that is, even if<br>
the desired slope is zero.<br>
> <br>
> Roger Stafford<br>
> <br>
<br>
No way! Roger, that's brilliant! thanks! I was kinda bummed<br>
that I couldn't have a horizontal slope with my old solution<br>
so this one has made me even happier. <br>
<br>
Thanks everyone!!

Tue, 22 Jul 2008 18:10:19 +0000
Re: Weighted random number generator
http://www.mathworks.com/matlabcentral/newsreader/view_thread/173000#444695
John D'Errico
"stinkinrich88 Coombez" <stinkinrich88@gmail.com> wrote in message <br>
<g64fa3$hc7$1@fred.mathworks.com>...<br>
> "John D'Errico" <woodchips@rochester.rr.com> wrote in<br>
> message <g64alt$1kr$1@fred.mathworks.com>...<br>
> > "stinkinrich88 Coombez" <stinkinrich88@gmail.com> wrote in<br>
> message <br>
> > <g647ip$2d5$1@fred.mathworks.com>...<br>
> > > Hello,<br>
> > > <br>
> > > I would like a function similar to the rnd function but I<br>
> > > want the generated numbers to be weighted, i.e. I would like<br>
> > > the function to be more likely to give me a higher number<br>
> > > than a lower number and I would like to be able to specify<br>
> > > this probability. <br>
> > > <br>
> > > For example, the betarnd function almost does what I want. I<br>
> > > can make a sloping probability distribution with it. The<br>
> > > only trouble is the probability distribution bulges slightly<br>
> > > in the middle and I want it to be straight. <br>
> > > <br>
> > > Is there any function I can use to do this? Or a function<br>
> > > that gives me random numbers (between 01) using a formula<br>
> > > that I specify?<br>
> > > <br>
> > > Thanks!<br>
> > <br>
> > While you can indeed generate your own<br>
> > random generator for a linear pdf, it is<br>
> > also trivial to use the stats toolbox and the<br>
> > beta distribution. You just need to choose<br>
> > the proper set of parameters.<br>
> > <br>
> > ezplot(@(x) betapdf(x,2,1),[0 1])<br>
> > <br>
> > John<br>
> <br>
> Thanks for your responses, everyone. I'm just getting my<br>
> head around them!<br>
> <br>
> John, your solution looks great, the only trouble is I need<br>
> to be able to adjust the steepness of the slope. I have<br>
> tried adjusting the parameters for betapdf but I end up with<br>
> a curve. Is there a way I can adjust the steepness of the<br>
> slop while keeping it flat? <br>
<br>
<br>
A pdf cannot have its slope "adjusted".<br>
<br>
That is defined by the requirement that<br>
your pdf MUST integrate to 1.<br>
<br>
You said you wanted a linear PDF. Do you<br>
intend that p(0) not be zero? This is the<br>
only degree of freedom that is available.<br>
<br>
Assume that your pdf is defined over [0,1].<br>
<br>
pdf(x) = a + b*x<br>
<br>
So we will define the value of the pdf at<br>
x = 0 as a. Remember that the integral<br>
of this function must be 1. So if we<br>
integrate this function<br>
<br>
a*x + b*x^2/2<br>
<br>
we come to the conclusion that<br>
<br>
a + b/2 = 1<br>
<br>
So the slope has only one possible value.<br>
<br>
b = 2*(1  a)<br>
<br>
The value of the right end point of the pdf<br>
must also be nonnegative.<br>
<br>
pdf(x) = a + 2*(1a)*x<br>
<br>
pdf(1) = a + 2  2*a = 2  a<br>
<br>
So we come to the conclusion that a linear<br>
pdf over the interval [0,1] must have its<br>
constant term (a) in the interval<br>
<br>
0 <= a <= 2<br>
<br>
The cdf is the integral. So we have<br>
<br>
cdf(x) = a*x + (1a)*x^2<br>
<br>
All possible linear pdfs with support on<br>
[0,1] must come from this family of pdfs.<br>
Now if you want to generate a random<br>
number from this distribution, just form<br>
the inverse. Generate a random number<br>
using rand.<br>
<br>
r = rand();<br>
<br>
Pick your favorite value for a. I'll use<br>
a = 1.5, and now solve for x.<br>
<br>
(1a)*x^2 + a*x  r = 0<br>
<br>
Lets do this for a large sample, then plot<br>
the histogram.<br>
<br>
a = 1.5;<br>
r = rand(1000000,1);<br>
x = (a + sqrt(a.^2 + 4*(1a)*r))./(2*(1a));<br>
hist(x,500)<br>
<br>
HTH,<br>
John

Sat, 26 Mar 2011 01:14:04 +0000
Re: Weighted random number generator
http://www.mathworks.com/matlabcentral/newsreader/view_thread/173000#827477
Tim
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?

Sat, 26 Mar 2011 10:27:02 +0000
Re: Weighted random number generator
http://www.mathworks.com/matlabcentral/newsreader/view_thread/173000#827511
stinkinrich88
"Tim " <junk@gmail.com> wrote in message <imjeks$hc8$1@fred.mathworks.com>...<br>
> 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? <br>
<br>
Hi Tim,<br>
<br>
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 AcceptanceRejection section on that page. Hope this helps you!

Tue, 29 Mar 2011 03:14:03 +0000
Re: Weighted random number generator
http://www.mathworks.com/matlabcentral/newsreader/view_thread/173000#827951
Roger Stafford
"Tim " <junk@gmail.com> wrote in message <imjeks$hc8$1@fred.mathworks.com>...<br>
> 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?<br>
         <br>
The identity cos(x)^2 = (1+cos(2*x))/2 implies that your distribution is the "raised cosine distribution". See<br>
<br>
<a href="http://en.wikipedia.org/wiki/Raised_cosine_distribution">http://en.wikipedia.org/wiki/Raised_cosine_distribution</a><br>
<br>
You can generate it without rejection using matlab's 'rand' function as follows:<br>
<br>
n = 1000; % Specify the number of random values to be generated<br>
r = 2*pi*(rand(n,1)1/2);<br>
r = sign(r).*abs(r).^(1/3);<br>
x = 1.7748*r+0.1726*r.^3; % Initial inverse estimate<br>
for k = 1:4 % Four iterations will suffice to make inverse precise<br>
dif = sign(x).*(abs(x)sin(abs(x))).^(1/3)  r; % Amount of error<br>
der = 0.5503210.0275161*x.^2+0.000327572*x.^4; % Approx. derivative<br>
x = x  dif./der; % Make Gauss correction<br>
end<br>
x = x+pi*(12*(x>=0)); % Swap halves<br>
<br>
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.<br>
<br>
Roger Stafford