Author Topic: The Random Number God: With Code in C.  (Read 2419 times)

Bear

  • High Priest
  • ****
  • Posts: 308
    • View Profile
The Random Number God: With Code in C.
« on: August 18, 2014, 05:08:41 PM »

New Blog post at http://dillingers.com/blog/2014/08/17/the-random-number-god/

It is:

A brief discussion of the role of PRNG's in game systems and why you'd want to code one instead of just assuming that your dev environment has one that's good enough (or that all your different dev environments are consistent enough in their choices).

A review of a bunch of system PRNG's (mostly bad ones)

A bunch of other PRNG's (mostly better ones that are just as easy to code)

And one fully coded library for random numbers in C, using a lagged-fibonacci generator with 99 32-bit words of state and maximal period.  All of these routines except the first four are easy to adapt to some other kind of PRNG. 

RNG_new  allocates and initializes a new pseudorandom generator. 

RNG_randomize  drinks randomness from /dev/urandom to initialize the generator.

RNG_Tweak  takes an integer argument and uses it to transform the state of an RNG so that the RNG produces a different sequence from that point onward.

RNG_Rand  is the fundamental building block; it uses the PRNG to produce a random number between zero (inclusive) and RNG_MAX (exclusive).

RNG_roll produces a roll uniformly distributed between 0 inclusive and some integer argument exclusive.

RNG_dieroll produces a roll uniformly distributed between some minimum inclusive and some maximum exclusive.

RNG_diesum produces a sum of dierolls, of a sort familiar and intuitive to lots of RPG gamers.

RNG_Uniform produces a double uniformly distributed in the range between a minimum argument and a maximum argument.

RNG_Gaussian takes a mean and standard deviation as a range and produces a double having a normal distribution (aka, a Gaussian distribution).

RNG_LimGaussian takes a mean, standard deviation, and a limit, and produces a double with a gaussian distribution but excludes results more than <limit> standard deviations away from the mean.

RNG_Contest takes two integer values and a dominance factor, then makes a random determination which of them should be regarded as “winning” in a given instance of a contest.  The bigger one is more likely to win the higher the dominance factor is.  It’s for things like hit rolls, strength contests, etc.

As usual for blog-published code, it's free for anybody to use.

Krice

  • Troll
  • Chancellor
  • *****
  • Posts: 2249
    • View Profile
    • Email
Re: The Random Number God: With Code in C.
« Reply #1 on: August 19, 2014, 03:01:18 PM »
How do you know so much about stuff?

Bear

  • High Priest
  • ****
  • Posts: 308
    • View Profile
Re: The Random Number God: With Code in C.
« Reply #2 on: August 19, 2014, 06:08:15 PM »
I have fully embraced my self-identity as an obsessive weirdo. 

Study is one of my basic forms of recreation and relaxation.