Xyris  0.5
rand.cpp
Go to the documentation of this file.
1 /**
2  * @file rand.cpp
3  * @author Brian Kernighan, Dennis Ritchie (C Standard Authors)
4  * @author Michel (JMallone) Gomes ([email protected])
5  * @brief Portable implementation of rand and srand as according to the C standard implementation by K&R.
6  * @version 0.1
7  * @date 2021-07-20
8  *
9  * @copyright C Programming Language copyright Brian Kernighan, Dennis Ritchie
10  * @copyright Implementation Copyright the Xyris Contributors (c) 2021.
11  *
12  * References:
13  * https://wiki.osdev.org/Random_Number_Generator#The_Standard.27s_Example
14  * https://pubs.opengroup.org/onlinepubs/9699919799/functions/rand.html
15  *
16  */
17 
18 #include <Library/rand.hpp>
19 
20 unsigned long _seedrand = 1;
21 
22 int rand(void)
23 {
24  _seedrand = _seedrand * 214013L + 2531011L;
25  return (unsigned int)(_seedrand >> 16) & MAX_RAND;
26 }
27 
28 void srand(unsigned int seed)
29 {
30  _seedrand = seed;
31 }
srand
void srand(unsigned int seed)
Set a seed to rand.
Definition: rand.cpp:28
_seedrand
unsigned long _seedrand
Definition: rand.cpp:20
MAX_RAND
#define MAX_RAND
Definition: rand.hpp:18
rand
int rand(void)
Return a random value.
Definition: rand.cpp:22
rand.hpp