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 "global.h"
5
6#include <assert.h>
7#include <stdbool.h>
8#include <stdint.h>
9
11
12typedef uint64_t generator;
13
14static inline uint64_t kissat_next_random64 (generator *rng) {
15 *rng *= 6364136223846793005ul;
16 *rng += 1442695040888963407ul;
17 return *rng;
18}
19
20static inline unsigned kissat_next_random32 (generator *rng) {
21 return kissat_next_random64 (rng) >> 32;
22}
23
24static inline unsigned kissat_pick_random (generator *rng, unsigned l,
25 unsigned r) {
26 CADICAL_assert (l <= r);
27 if (l == r)
28 return l;
29 const unsigned delta = r - l;
30 const unsigned tmp = kissat_next_random32 (rng);
31 const double fraction = tmp / 4294967296.0;
32 CADICAL_assert (0 <= fraction), CADICAL_assert (fraction < 1);
33 const unsigned scaled = delta * fraction;
34 CADICAL_assert (scaled < delta);
35 const unsigned res = l + scaled;
36 CADICAL_assert (l <= res), CADICAL_assert (res < r);
37 return res;
38}
39
40static inline bool kissat_pick_bool (generator *rng) {
41 return kissat_pick_random (rng, 0, 2);
42}
43
44static inline double kissat_pick_double (generator *rng) {
45 return kissat_next_random32 (rng) / 4294967296.0;
46}
47
49
50#endif
#define ABC_NAMESPACE_HEADER_END
#define ABC_NAMESPACE_HEADER_START
NAMESPACES ///.
#define CADICAL_assert(ignore)
Definition global.h:14
ABC_NAMESPACE_HEADER_START typedef uint64_t generator
Definition random.h:12