ABC: A System for Sequential Synthesis and Verification
 
Loading...
Searching...
No Matches
random.h
Go to the documentation of this file.
1#ifndef _random_h_INCLUDED
2#define _random_h_INCLUDED
3
4#include <assert.h>
5#include <stdbool.h>
6#include <stdint.h>
7
8#include "global.h"
10
11typedef uint64_t generator;
12
13static inline uint64_t kissat_next_random64 (generator *rng) {
14 *rng *= 6364136223846793005ul;
15 *rng += 1442695040888963407ul;
16 return *rng;
17}
18
19static inline unsigned kissat_next_random32 (generator *rng) {
20 return kissat_next_random64 (rng) >> 32;
21}
22
23static inline unsigned kissat_pick_random (generator *rng, unsigned l,
24 unsigned r) {
25 KISSAT_assert (l <= r);
26 if (l == r)
27 return l;
28 const unsigned delta = r - l;
29 const unsigned tmp = kissat_next_random32 (rng);
30 const double fraction = tmp / 4294967296.0;
31 KISSAT_assert (0 <= fraction), KISSAT_assert (fraction < 1);
32 const unsigned scaled = delta * fraction;
33 KISSAT_assert (scaled < delta);
34 const unsigned res = l + scaled;
35 KISSAT_assert (l <= res), KISSAT_assert (res < r);
36 return res;
37}
38
39static inline bool kissat_pick_bool (generator *rng) {
40 return kissat_pick_random (rng, 0, 2);
41}
42
43static inline double kissat_pick_double (generator *rng) {
44 return kissat_next_random32 (rng) / 4294967296.0;
45}
46
48
49#endif
#define ABC_NAMESPACE_HEADER_END
#define ABC_NAMESPACE_HEADER_START
NAMESPACES ///.
ABC_NAMESPACE_HEADER_START typedef uint64_t generator
Definition random.h:12
#define KISSAT_assert(ignore)
Definition global.h:13