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$>
References: <g647ip$2d5$> <g64alt$1kr$> <g64fa3$hc7$> <g657qb$k2g$> <imjeks$hc8$>
Reply-To: <HIDDEN>
Content-Type: text/plain; charset=UTF-8; format=flowed
Content-Transfer-Encoding: 8bit
X-Trace: 1301368443 28631 (29 Mar 2011 03:14:03 GMT)
NNTP-Posting-Date: Tue, 29 Mar 2011 03:14:03 +0000 (UTC)
X-Newsreader: MATLAB Central Newsreader 1187260
Xref: comp.soft-sys.matlab:718708

"Tim " <> wrote in message <imjeks$hc8$>...
> 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

  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
 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