ABC: A System for Sequential Synthesis and Verification
 
Loading...
Searching...
No Matches
random.hpp
Go to the documentation of this file.
1#ifndef _random_hpp_INCLUDED
2#define _random_hpp_INCLUDED
3
4#include "global.h"
5
6#include <cstdint>
7
9
10// Random number generator.
11
12namespace CaDiCaL {
13
14class Random {
15
16 uint64_t state;
17
18 void add (uint64_t a) {
19 if (!(state += a))
20 state = 1;
21 next ();
22 }
23
24public:
25 // Without argument use a machine, process and time dependent seed.
26 //
27 Random ();
28
29 Random (uint64_t seed) : state (seed) {}
30 void operator= (uint64_t seed) { state = seed; }
31 Random (const Random &other) : state (other.seed ()) {}
32
33 void operator+= (uint64_t a) { add (a); }
34 uint64_t seed () const { return state; }
35
36 uint64_t next () {
37 state *= 6364136223846793005ul;
38 state += 1442695040888963407ul;
39 CADICAL_assert (state);
40 return state;
41 }
42
44 next ();
45 return state >> 32;
46 }
47 int generate_int () { return (int) generate (); }
48 bool generate_bool () { return generate () < 2147483648u; }
49
50 // Generate 'double' value in the range '[0,1]' excluding '1'.
51 //
52 double generate_double () { return generate () / 4294967295.0; }
53
54 // Generate 'int' value in the range '[l,r]'.
55 //
56 int pick_int (int l, int r) {
57 CADICAL_assert (l <= r);
58 const unsigned delta = 1 + r - (unsigned) l;
59 unsigned tmp = generate (), scaled;
60 if (delta) {
61 const double fraction = tmp / 4294967296.0;
62 scaled = delta * fraction;
63 } else
64 scaled = tmp;
65 const int res = scaled + l;
66 CADICAL_assert (l <= res);
67 CADICAL_assert (res <= r);
68 return res;
69 }
70
71 int pick_log (int l, int r) {
72 CADICAL_assert (l <= r);
73 const unsigned delta = 1 + r - (unsigned) l;
74 int log_delta = delta ? 0 : 32;
75 while (log_delta < 32 && (1u << log_delta) < delta)
76 log_delta++;
77 const int log_res = pick_int (0, log_delta);
78 unsigned tmp = generate ();
79 if (log_res < 32)
80 tmp &= (1u << log_res) - 1;
81 if (delta)
82 tmp %= delta;
83 const int res = l + tmp;
84 CADICAL_assert (l <= res), CADICAL_assert (res <= r);
85 return res;
86 }
87
88 // Generate 'double' value in the range '[l,r]'.
89 //
90 double pick_double (double l, double r) {
91 CADICAL_assert (l <= r);
92 double res = (r - l) * generate_double ();
93 res += l;
94 CADICAL_assert (l <= res);
95 CADICAL_assert (res <= r);
96 return res;
97 }
98};
99
100} // namespace CaDiCaL
101
103
104#endif
unsigned int uint32_t
Definition Fxch.h:32
#define ABC_NAMESPACE_CXX_HEADER_START
#define ABC_NAMESPACE_CXX_HEADER_END
#define CADICAL_assert(ignore)
Definition global.h:14
void operator+=(uint64_t a)
Definition random.hpp:33
uint64_t next()
Definition random.hpp:36
int generate_int()
Definition random.hpp:47
Random(uint64_t seed)
Definition random.hpp:29
double pick_double(double l, double r)
Definition random.hpp:90
void operator=(uint64_t seed)
Definition random.hpp:30
bool generate_bool()
Definition random.hpp:48
double generate_double()
Definition random.hpp:52
int pick_log(int l, int r)
Definition random.hpp:71
uint32_t generate()
Definition random.hpp:43
Random(const Random &other)
Definition random.hpp:31
uint64_t seed() const
Definition random.hpp:34
int pick_int(int l, int r)
Definition random.hpp:56