-
Notifications
You must be signed in to change notification settings - Fork 1.1k
/
Copy pathsb_rand.h
74 lines (58 loc) · 2.14 KB
/
sb_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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
/*
Copyright (C) 2016-2017 Alexey Kopytov <[email protected]>
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*/
#ifndef SB_RAND_H
#define SB_RAND_H
#include <stdlib.h>
#include "xoroshiro128plus.h"
/* Random numbers distributions */
typedef enum
{
DIST_TYPE_UNIFORM,
DIST_TYPE_GAUSSIAN,
DIST_TYPE_PARETO,
DIST_TYPE_ZIPFIAN
} rand_dist_t;
typedef uint64_t sb_rng_state_t [2];
/* optional seed set on the command line */
extern int sb_rand_seed;
/* Thread-local RNG state */
extern TLS sb_rng_state_t sb_rng_state;
/* Return a uniformly distributed pseudo-random 64-bit unsigned integer */
inline uint64_t sb_rand_uniform_uint64(void)
{
return xoroshiro_next(sb_rng_state);
}
/* Return a uniformly distributed pseudo-random double in the [0, 1) interval */
inline double sb_rand_uniform_double(void)
{
const uint64_t x = sb_rand_uniform_uint64();
const union { uint64_t i; double d; } u = { .i = UINT64_C(0x3FF) << 52 | x >> 12 };
return u.d - 1.0;
}
int sb_rand_register(void);
void sb_rand_print_help(void);
int sb_rand_init(void);
void sb_rand_done(void);
void sb_rand_thread_init(void);
/* Generator functions */
uint64_t sb_rand_default(uint64_t, uint64_t);
uint64_t sb_rand_uniform(uint64_t, uint64_t);
uint64_t sb_rand_gaussian(uint64_t, uint64_t);
uint64_t sb_rand_pareto(uint64_t, uint64_t);
uint64_t sb_rand_zipfian(uint64_t, uint64_t);
uint64_t sb_rand_unique(void);
void sb_rand_str(const char *, char *);
uint64_t sb_rand_varstr(char *, uint64_t, uint64_t);
#endif /* SB_RAND_H */