random.h is a header only random number generation library for games, image processing and procedural content generation.

A few years back I wrote some handy floating point random number generation routines for a raytracer.  Time and again, I have needed these routines for other programs I have been working on.  It seems like a good idea to wrap them up in a small header and release them in the public domain for others to use too.

Each RNG function takes an 64bit integer as a seed value and permutes it in place.  The resulting bits are used for the generation of the result.  I like this design as it keeps the RNG functions stateless and thread safe.  You just have to keep track of the uint64_t you have been using as your RNG stream.

Included are various 1, 2 and 3 dimentional random number generators with a range of different distributions.

Triangular random noise is very usefull when dithering audio and images during the process of bitdepth conversion.  Typicaly, triangular noise is added to the signal immediately before type casting (at the same magnitude as the LSB of the destination bit depth).

The 2d and 3d vector RNG functions generate statisticaly unbiased vectors inside and on the unit circle or sphere (Generating vectors by randomizing the components and normalizing would bias the vector to the corners of unit cube).  I have used these functions successfully in the past for generating ambient occlusion maps.

I wrote a little program to generate some images to display the output distribution of each function.  A 3d point is generated by filling its component from the RNG function under test, and the resulting point is projected in space, as well as projected on each component axis (the red, blue and green planes).  Hopefully the images help a little to convey the distribution of each function.

Again, you can find the library here:

randfu() - unsigned random 
range [0,1]


randfs() - signed random 
range [-1,+1]


trandfs() - triangular random 
range[-1,+1], tends to 0


grandfs() - gaussian random 
range ~[-1,+1], tends to 0


vrand3d() - 3d vector random 
magnitude range: [0, 1]


nvrand3d() - normalized 3d vector random 
magnitude = 1


vrand2d() - 2d vector random
magnitude range: [0,1]


nvrand2d() - normalized 2d vector random
magnitude = 1


Binding Tiny C Compiler

Years ago I found a interesting library called TinyCC, which provided a simple and small C compiler with one major twist…  it can be embedded inside another application.  Because of this, a program can compile new functions for itself at run time and call them using function pointers.  In a sense it can be used like an immensely powerful scripting language, with the benefit that there is no speed hit due to interpretation or emulation.  Its weakness as a scripting language is that you have to account for portability and security problems due to the execution of these ‘scripts’ running as native code on the host processor.

So far, I have used TinyCC for a few projects in the past and recently in a texture generator as well as my text adventure engine.  The reason for this post however, is that TinyCC is not the most intuitive thing to embed into a windows application, so I wanted to take a moment to explain one clean method to do so.

There is no static library for use with visual C++ so that option is out, however since a fully compiled DLL file is included in the download package, it seemed that it would be easiest to dynamically link the DLL through code at run time.  This requires loading the DLL and requesting all of the functions that are exported from it which is a fairly easy operation on any platform and generally differnt OS’s have much the same interface for doing so. The only tricky part is actually specifying all of the annoying function pointer casts correctly during binding.

Without farther talk, here is some straight forward code to load TinyCC and bind the interface for you. Which I hope it should all be fairly straight forward and easy to understand.



Methods for sinwave aproximation


As you may have gathered from my previous posts, I like to use fixed point arithmetic where possible. There are a number of reasons for this, speed being one, but also some processors even in this day and age don’t have and kind of FPU on them. This makes fixed point the only viable solution for fractional maths on these kind of machines. I own a Dingoo A320 handled console and it is just one such machine, running a Mips processor without any FPU. I believe that the NintendoDS is another such machine, but I cant say for sure since I haven’t programmed for on yet.

I need a fast way to perform rotations in 2D, which is a process requiring fractional maths and access to the sine and cosine function.  I also need the sin functions for programming a digital low pass filter in an audio synthesizer.  The sine and cosine implementation in the standard library are slow, mostly because they choose accuracy over speed, and they also work exclusively with floating point values.  I wanted to find a accurate and fast method for getting sin-wave values, without using floating point values.  After some research I managed to put together a small library containing a few different ways of making these approximations, very fast and with various degrees of accuracy.

So what is in this library:

  • Six different tables, each encoding a sin-wave with different sizes and resolution.
  • Tables are each a power of two in size, providing optimization opportunities.
  • Tables are in 8bit and 16bit resolution.
  • A function to index one of the tables using linear interpolation.
  • A very nice and fast approximate sin-wave function that performs more accurately (and quicker I expect) then the table based methods.
  • Additional Cosine functions using the same methods.
  • Wrapping for indices that lie outside of the sin-waves period.
  • It has been tested a little and they all seem to work nicely.

Source Code: