2018-10-14 19:28:18 +08:00
|
|
|
/*
|
|
|
|
* Copyright (c) 2006-2018, RT-Thread Development Team
|
|
|
|
*
|
|
|
|
* SPDX-License-Identifier: Apache-2.0
|
|
|
|
*
|
|
|
|
* Change Logs:
|
|
|
|
* Date Author Notes
|
|
|
|
*/
|
2010-10-16 07:31:11 +08:00
|
|
|
#include <stdlib.h>
|
2010-10-19 16:18:24 +08:00
|
|
|
#include <stdint.h>
|
2010-10-16 07:31:11 +08:00
|
|
|
#include <sys/types.h>
|
|
|
|
|
2012-11-12 10:08:27 +08:00
|
|
|
static unsigned int _seed=1;
|
2010-10-16 07:31:11 +08:00
|
|
|
|
|
|
|
/* Knuth's TAOCP section 3.6 */
|
|
|
|
#define M ((1U<<31) -1)
|
|
|
|
#define A 48271
|
|
|
|
#define Q 44488 // M/A
|
|
|
|
#define R 3399 // M%A; R < Q !!!
|
|
|
|
|
|
|
|
int rand_r(unsigned int* seed)
|
|
|
|
{ int32_t X;
|
|
|
|
|
|
|
|
X = *seed;
|
|
|
|
X = A*(X%Q) - R * (int32_t) (X/Q);
|
|
|
|
if (X < 0)
|
|
|
|
X += M;
|
|
|
|
|
|
|
|
*seed = X;
|
|
|
|
return X;
|
|
|
|
}
|
|
|
|
|
|
|
|
int rand(void) {
|
2012-11-12 10:08:27 +08:00
|
|
|
return rand_r(&_seed);
|
2010-10-16 07:31:11 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void srand(unsigned int i)
|
|
|
|
{
|
2012-11-12 10:08:27 +08:00
|
|
|
_seed=i;
|
2010-10-16 07:31:11 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
int random(void) __attribute__((alias("rand")));
|
|
|
|
void srandom(unsigned int i) __attribute__((alias("srand")));
|