Post Reply 
Using the Raised Cosine Distribution in PRNG
12-18-2016, 01:51 AM (This post was last modified: 12-18-2016 04:13 AM by Namir.)
Post: #1
Using the Raised Cosine Distribution in PRNG
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
Find all posts by this user
Quote this message in a reply
12-21-2016, 01:42 PM
Post: #2
RE: Using the Raised Cosine Distribution in PRNG
I ever heard of this distribution. What is it used to model?
Find all posts by this user
Quote this message in a reply
12-21-2016, 03:20 PM
Post: #3
RE: Using the Raised Cosine Distribution in PRNG
(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
Find all posts by this user
Quote this message in a reply
12-25-2016, 12:30 PM
Post: #4
RE: Using the Raised Cosine Distribution in PRNG
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.
Find all posts by this user
Quote this message in a reply
12-26-2016, 01:37 AM
Post: #5
RE: Using the Raised Cosine Distribution in PRNG
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.
Find all posts by this user
Quote this message in a reply
12-26-2016, 01:42 AM
Post: #6
RE: Using the Raised Cosine Distribution in PRNG
For exact normal distribution samples from uniforms use the Box–Muller transform.

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

Pauli
Find all posts by this user
Quote this message in a reply
12-26-2016, 02:12 AM (This post was last modified: 12-26-2016 02:19 AM by Namir.)
Post: #7
RE: Using the Raised Cosine Distribution in PRNG
(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
Find all posts by this user
Quote this message in a reply
12-26-2016, 07:39 AM
Post: #8
RE: Using the Raised Cosine Distribution in PRNG
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
Find all posts by this user
Quote this message in a reply
12-26-2016, 11:03 AM
Post: #9
RE: Using the Raised Cosine Distribution in PRNG
(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!!

:-)
Find all posts by this user
Quote this message in a reply
12-26-2016, 08:01 PM
Post: #10
RE: Using the Raised Cosine Distribution in PRNG
(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
Find all posts by this user
Quote this message in a reply
Post Reply 




User(s) browsing this thread: 2 Guest(s)