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

Subversion Repositories openrisc_me

[/] [openrisc/] [trunk/] [gnu-src/] [newlib-1.18.0/] [newlib/] [libc/] [stdlib/] [rand_r.c] - Blame information for rev 258

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

Line No. Rev Author Line
1 207 jeremybenn
#include <stdlib.h>
2
 
3
/* Pseudo-random generator based on Minimal Standard by
4
   Lewis, Goodman, and Miller in 1969.
5
 
6
   I[j+1] = a*I[j] (mod m)
7
 
8
   where a = 16807
9
         m = 2147483647
10
 
11
   Using Schrage's algorithm, a*I[j] (mod m) can be rewritten as:
12
 
13
     a*(I[j] mod q) - r*{I[j]/q}      if >= 0
14
     a*(I[j] mod q) - r*{I[j]/q} + m  otherwise
15
 
16
   where: {} denotes integer division
17
          q = {m/a} = 127773
18
          r = m (mod a) = 2836
19
 
20
   note that the seed value of 0 cannot be used in the calculation as
21
   it results in 0 itself
22
*/
23
 
24
int
25
_DEFUN (rand_r, (seed), unsigned int *seed)
26
{
27
        long k;
28
        long s = (long)(*seed);
29
        if (s == 0)
30
          s = 0x12345987;
31
        k = s / 127773;
32
        s = 16807 * (s - k * 127773) - 2836 * k;
33
        if (s < 0)
34
          s += 2147483647;
35
        (*seed) = (unsigned int)s;
36
        return (int)(s & RAND_MAX);
37
}

powered by: WebSVN 2.1.0

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