HP Forums

Full Version: Using the Raised Cosine Distribution in PRNG
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
The Raised Cosine Distribution provides tool for a simple non-uniform PRNG. The equation for the cumulative probability function is:

CDF = [1 + xs + sin(pi*xs)/pi]/2

Where xs = (x-m)/s and m is the mean and s is the spread.

Choosing m = s = 0.5 makes the CDF curve map values from the range (0, 1) to values in the range (0, 1). This special mapping allows us to skip the traditional approach where we assign a uniform random number to CDF and solve for x (which requires using an iterative method). Instead we can cheat and use the above equation by assigning a uniform random number to x and calculate the CDF as the specialized random number. The distribution of the random number generated form a U shaped distribution. So to make your games interesting, test for values generated around 0.5 (say the range (0.45, 0.55)) to trigger game events. The recommended range is less likely to occur than values near 0 or 1.

I know that my suggestion is heretical, but it works!!

Namir
I ever heard of this distribution. What is it used to model?
(12-21-2016 01:42 PM)mark4flies Wrote: [ -> ]I ever heard of this distribution. What is it used to model?

I think it's a quick and rough approximation for Normal Gaussian distribution.

Namir
Thanks!
I agree with the "rough" performance assessment. Would not serve serious applications that require PRNG. But a game would not suffer while enjoying the speed.
Another distribution (probably suitable for gaming but not for high-accuracy calculation without some extra work) that mimics the normal is the Kahn Pseudo Normal. It's easy to generate. With u being uniformly distributed between 0 and 1, the variates are given by:

Y = Log ( 1/u - 1 )

Another possibility, but with infinite variance:

Y = Tangent( Pi * (Y - 1/2) )

Averages of the first distribution converge to a normal distribution (central limit theorem). Averages of the second distribution converge to another Cauchy distribution.
For exact normal distribution samples from uniforms use the Box–Muller transform.

Two independent uniform samples in, two independent normal samples out.

Pauli
(12-26-2016 01:42 AM)Paul Dale Wrote: [ -> ]For exact normal distribution samples from uniforms use the Box–Muller transform.

Two independent uniform samples in, two independent normal samples out.

Pauli

The Raised Cosine Distribution uses just one call to a trig function per pseudo-random number. No need for calling ln(x) or sqrt(x) as is the case with the Box-Muller method. Granted that this method requires straight forward calculations.

Namir
On calculators that support it:

\( RAND \\ \phi^{-1}\)

works really well. It might even be faster than the programme steps for the raised cosine distribution -- depending on the approximation used for \( \phi^{-1}\)


Pauli
(12-26-2016 07:39 AM)Paul Dale Wrote: [ -> ]On calculators that support it:

\( RAND \\ \phi^{-1}\)

works really well. It might even be faster than the programme steps for the raised cosine distribution -- depending on the approximation used for \( \phi^{-1}\)


Pauli

That's an offer I can't refuse!!

:-)
(12-26-2016 07:39 AM)Paul Dale Wrote: [ -> ]On calculators that support it:

\( RAND \\ \phi^{-1}\)

works really well. It might even be faster than the programme steps for the raised cosine distribution -- depending on the approximation used for \( \phi^{-1}\)

If you refer to the 34s: The Normal quantile is evaluated by an initial first guess which is then refined in one or (at most) two iterations. Since every iteration calls the Incomplete Gamma function which is a bit slow for arguments > √3, a faster simplified approach could be used that only applies the first guess to the random number. At least for the suggested application in games this should be sufficiently exact. But the Normal guess routine is not exposed to the user so the suggested method is not applicable. At least not directly. ;-)

Dieter
Reference URL's