Path: news.mathworks.com!not-for-mail
From: <HIDDEN>
Newsgroups: comp.soft-sys.matlab
Subject: Re: Weighted random number generator
Date: Tue, 29 Mar 2011 03:14:03 +0000 (UTC)
Organization: The MathWorks, Inc.
Lines: 23
Message-ID: <imripr$run$1@fred.mathworks.com>
References: <g647ip$2d5$1@fred.mathworks.com> <g64alt$1kr$1@fred.mathworks.com> <g64fa3$hc7$1@fred.mathworks.com> <g657qb$k2g$1@fred.mathworks.com> <imjeks$hc8$1@fred.mathworks.com>
Reply-To: <HIDDEN>
NNTP-Posting-Host: www-00-blr.mathworks.com
Content-Type: text/plain; charset=UTF-8; format=flowed
Content-Transfer-Encoding: 8bit
X-Trace: fred.mathworks.com 1301368443 28631 172.30.248.45 (29 Mar 2011 03:14:03 GMT)
X-Complaints-To: news@mathworks.com
NNTP-Posting-Date: Tue, 29 Mar 2011 03:14:03 +0000 (UTC)
X-Newsreader: MATLAB Central Newsreader 1187260
Xref: news.mathworks.com comp.soft-sys.matlab:718708

"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