cleanup the random generation function usage

Updated and simplified patch. It fixes handling of /dev/urandom errors, adds a single call to srandom(), uses random() everywhere and adds support for getentropy().