OpenCores
URL https://opencores.org/ocsvn/or1k/or1k/trunk

Subversion Repositories or1k

[/] [or1k/] [trunk/] [newlib-1.10.0/] [newlib/] [libc/] [stdlib/] [rand_r.c] - Rev 1773

Go to most recent revision | Compare with Previous | Blame | View Log

#include <stdlib.h>
 
/* Pseudo-random generator based on Minimal Standard by
   Lewis, Goodman, and Miller in 1969.
 
   I[j+1] = a*I[j] (mod m)
 
   where a = 16807
         m = 2147483647
 
   Using Schrage's algorithm, a*I[j] (mod m) can be rewritten as:
 
     a*(I[j] mod q) - r*{I[j]/q}      if >= 0
     a*(I[j] mod q) - r*{I[j]/q} + m  otherwise
 
   where: {} denotes integer division 
          q = {m/a} = 127773 
          r = m (mod a) = 2836
 
   note that the seed value of 0 cannot be used in the calculation as
   it results in 0 itself
*/
 
int
_DEFUN (rand_r, (seed), unsigned int *seed)
{
        long k;
        long s = (long)(*seed);
        if (s == 0)
          s = 0x12345987;
        k = s / 127773;
        s = 16807 * (s - k * 127773) - 2836 * k;
        if (s < 0)
          s += 2147483647;
        (*seed) = (unsigned int)s;
        return (int)(s & RAND_MAX);
}
 

Go to most recent revision | Compare with Previous | Blame | View Log

powered by: WebSVN 2.1.0

© copyright 1999-2024 OpenCores.org, equivalent to Oliscience, all rights reserved. OpenCores®, registered trademark.