- This is a BAD RANDOM NUMBER GENERATOR!
- Under no circumstances should this be used in production code.
- This is just me messing around with C++ pointers.
- I am fully aware of how bad the code is. The point of this was just to implement something I thought might be funny. (segfault-based random numbers).
BadRandom is a memory access violation-based random number generator. It does the following to generate a number:
- Finds an accessible memory location by pseudo-randomly generating pointers until one points to an accessible memory address.
- Steps the newly-generated pointer forward until it hits an inaccessible address.
- Returns the number of steps, normalized to fit a given range.
Please note that this makes use of an incredibly janky compiler-specific hack (bug?) based on this StackOverflow answer to 'catch' and respond to access violations. This is implemented in
DumbRandom.cppas theGuard()method.Due to this hack, it may not work on some compilers, systems, builds, etc. Use at your own risk!
NOT EFFICIENT -- may not even generate one number in a year due to picking a memory location at random. 64-bit systems have around 1.8e+19 possible addresses, so the odds of it picking a valid one are slim to none.
If you would like to contribute to this inefficiency (or try to make it efficient), please do so!