-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmath_rand.h
59 lines (47 loc) · 876 Bytes
/
math_rand.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
#ifndef MATH_RAND_MATH_H
#define MATH_RAND_MATH_H
#include "math_m.h"
#include <stdlib.h> // rand & srand
#ifdef __cplusplus
extern "C" {
#endif
// @DOC: seed rand, call before other calls to rand
M_INLINE void rand_seed(unsigned int seed)
{
srand(seed);
}
M_INLINE int rand_max()
{
return RAND_MAX;
}
M_INLINE int rand_int()
{
return rand();
}
M_INLINE float rand_f32()
{
return (float)rand() / (float)RAND_MAX;
}
#define rand_float() rand_f32()
M_INLINE int rand_int_range(int min, int max)
{
return (int)(rand_f32() * (float)(max - min)) + min;
}
M_INLINE bool rand_bool()
{
return rand_int_range(0, 2) == 1;
}
M_INLINE uint64_t rand_u64()
{
uint64_t g = 0;
g += (uint64_t)rand_int();
g *= (uint64_t)rand_int();
g *= (uint64_t)rand_int();
g *= (uint64_t)rand_int();
g *= 2;
return g;
}
#ifdef __cplusplus
} // extern C
#endif
#endif