A fast random number generator in C
If you are using RNGs (Random Number Generators) for cryptography then you need one that has been validated for sufficient randomness. For example the libsodium library.
But if you want a fast one for general purpose use, then xoshiro256++ is a fast one. It’s actually that short that I can list it here. Just call next() and mod it (%) with the highest value+1. E.G. for a dice roll,
int diceroll= (next() %6) +1;
This algorithm is very fast, typically on the order of nano-seconds 10-9 seconds.
#include <stdint.h>
uint64_t rngstate[4];
static inline uint64_t rotl(const uint64_t x, int k) {
return (x << k) | (x >> (64 - k));
}
// Returns a Uint64 random number
uint64_t next(void) {
const uint64_t result = rotl(rngstate[0] + rngstate[3], 23) + rngstate[0];
const uint64_t t = rngstate[1] << 17;
rngstate[2] ^= rngstate[0];
rngstate[3] ^= rngstate[1];
rngstate[1] ^= rngstate[2];
rngstate[0] ^= rngstate[3];
rngstate[2] ^= t;
rngstate[3] = rotl(rngstate[3], 45);
return result;
}
At some time or other you are going to need a binary editor, to let you look in files and see what they contain.
Here’s it on 
I needed this in a bit of debug code. I wanted it to work like printf where there’s a format string containing one or more % format specifications and then write this into a buffer and dump it where ever.
So after yesterday’s post I also installed Code::Blocks on Ubuntu 20.04 LTS, the recent six monthly Ubuntu release. Guess what, it’s a much newer version of Code::Blocks that looks slightly different and does include SDL2. Although the demo program it creates is C++ not C (That coloured bar picture is the demo). I haven’t used it enough to see what’s different between this and version 16.01.

I’ve done 70 odd blog posts so far since I started this in March 2020 and there’s a fair number of gems and nuggets in there. Finding them though is probably a bit of hassle, so to let you see them easily, I’ve created a page of tips, accessible from 