implementation of rand()

后端 未结 11 1374
遥遥无期
遥遥无期 2020-12-01 01:34

I am writing some embedded code in C and need to use the rand() function. Unfortunately, rand() is not supported in the library for the controller. I need a simple implement

相关标签:
11条回答
  • 2020-12-01 02:06

    Here is a link to a ANSI C implementation of a few random number generators.

    0 讨论(0)
  • 2020-12-01 02:08

    Mersenne twister

    A bit from Wikipedia:

    • It was designed to have a period of 219937 − 1 (the creators of the algorithm proved this property). In practice, there is little reason to use a larger period, as most applications do not require 219937 unique combinations (219937 is approximately 4.3 × 106001; this is many orders of magnitude larger than the estimated number of particles in the observable universe, which is 1080).
    • It has a very high order of dimensional equidistribution (see linear congruential generator). This implies that there is negligible serial correlation between successive values in the output sequence.
    • It passes numerous tests for statistical randomness, including the Diehard tests. It passes most, but not all, of the even more stringent TestU01 Crush randomness tests.

    • source code for many languages available on the link.

    0 讨论(0)
  • 2020-12-01 02:09

    I found this: Simple Random Number Generation, by John D. Cook.

    It should be easy to adapt to C, given that it's only a few lines of code.

    Edit: and you could clarify what you mean by "relatively high-quality". Are you generating encryption keys for nuclear launch codes, or random numbers for a game of poker?

    0 讨论(0)
  • 2020-12-01 02:09

    The standard solution is to use a linear feedback shift register.

    0 讨论(0)
  • 2020-12-01 02:13

    There is one simple RNG named KISS, it is one random number generator according to three numbers.

    /* Implementation of a 32-bit KISS generator which uses no multiply instructions */ 
    
    static unsigned int x=123456789,y=234567891,z=345678912,w=456789123,c=0; 
    
    unsigned int JKISS32() { 
        int t; 
    
        y ^= (y<<5); y ^= (y>>7); y ^= (y<<22); 
    
        t = z+w+c; z = w; c = t < 0; w = t&2147483647; 
    
        x += 1411392427; 
    
        return x + y + w; 
    }
    

    Also there is one web site to test RNG http://www.phy.duke.edu/~rgb/General/dieharder.php

    0 讨论(0)
  • 2020-12-01 02:21

    I've made a collection of random number generators, "simplerandom", that are compact and suitable for embedded systems. The collection is available in C and Python.

    I've looked around for a bunch of simple and decent ones I could find, and put them together in a small package. They include several Marsaglia generators (KISS, MWC, SHR3), and a couple of L'Ecuyer LFSR ones.

    All the generators return an unsigned 32-bit integer, and typically have a state made of 1 to 4 32-bit unsigned integers.

    Interestingly, I found a few issues with the Marsaglia generators, and I've tried to fix/improve all those issues. Those issues were:

    • SHR3 generator (component of Marsaglia's 1999 KISS generator) was broken.
    • MWC low 16 bits have only an approx 229.1 period. So I made a slightly improved MWC, which gives the low 16 bits a 259.3 period, which is the overall period of this generator.

    I uncovered a few issues with seeding, and tried to make robust seeding (initialisation) procedures, so they won't break if you give them a "bad" seed value.

    0 讨论(0)
提交回复
热议问题