bugfixes and a better random generator stolen from stb.h