source: trunk/sys/dietlibc/rand_r.c @ 333

Last change on this file since 333 was 1, checked in by alain, 7 years ago

First import

File size: 545 bytes
Line 
1/*
2   Copyright (C) 2002 Luc Van Oostenryck
3
4   This is free software. You can redistribute and
5   modify it under the terms of the GNU General Public
6   Public License.
7*/
8
9#include <stdlib.h>
10#include <stdint.h>
11
12/* Knuth's TAOCP section 3.6 */
13#define M       ((1U<<31) -1)
14#define A       48271
15#define Q       44488           // M/A
16#define R       3399            // M%A; R < Q !!!
17
18// FIXME: ISO C/SuS want a longer period
19
20int rand_r(unsigned int* seed)
21{   int32_t X;
22
23    X = *seed;
24    X = A*(X%Q) - R * (int32_t) (X/Q);
25    if (X < 0)
26        X += M;
27
28    *seed = X;
29    return X;
30}
Note: See TracBrowser for help on using the repository browser.