1 /*
2  * Copyright (c) 2021 Space Cubics, LLC.
3  *
4  * SPDX-License-Identifier: Apache-2.0
5  */
6 
7 #include <stdlib.h>
8 #include <zephyr/sys/libc-hooks.h>
9 
10 #define OUTPUT_BITS (0x7fffffffU)
11 #define MULTIPLIER (1103515245U)
12 #define INCREMENT (12345U)
13 
rand_r(unsigned int * seed)14 int rand_r(unsigned int *seed)
15 {
16 	*seed = (MULTIPLIER * *seed + INCREMENT) & OUTPUT_BITS;
17 
18 	return *seed;
19 }
20 
21 #ifdef CONFIG_MINIMAL_LIBC_NON_REENTRANT_FUNCTIONS
22 static Z_LIBC_DATA unsigned int srand_seed = 1;
23 
srand(unsigned int s)24 void srand(unsigned int s)
25 {
26 	srand_seed = s;
27 }
28 
rand(void)29 int rand(void)
30 {
31 	return rand_r(&srand_seed);
32 }
33 #endif /* CONFIG_MINIMAL_LIBC_NON_REENTRANT_FUNCTIONS */
34