C Random Number Generation (pure C code, no libraries or functions)

后端 未结 7 1434
时光取名叫无心
时光取名叫无心 2021-01-21 03:11

I need to generate some random numbers in C for testing and debugging the system. The system is a custom hardware (SoC) with a limited set of functions so I can only use basic m

7条回答
  •  半阙折子戏
    2021-01-21 03:22

    A random number generator is basically a special* hash function which runs recursively from a starting seed.

    I've used the MurmurHash2 algorithm in my C# code to good effect. It's extremely fast and simple to implement and has been tested to be very well distributed with low collision rates. The project has several different open source hash functions written in C++ which should be easily convertible to C.


    * By special I mean that running the hash function on a value should return another seemingly random (but determinate) value, so that the output doesn't appear to form patterns. Also, the distribution of the returned value should have a uniform distribution.

提交回复
热议问题