1 /********************* */
4 ** Top contributors (to current version):
6 ** This file is part of the CVC4 project.
7 ** Copyright (c) 2009-2017 by the authors listed in the file AUTHORS
8 ** in the top-level source directory) and their institutional affiliations.
9 ** All rights reserved. See the file COPYING in the top-level source
10 ** directory for licensing information.\endverbatim
12 ** \brief A Random Number Generator.
14 ** A random number generator, implements the xorshift* generator
15 ** (see S. Vigna, An experimental exploration of Marsaglia's xorshift
16 ** generators, scrambled. ACM Trans. Math. Softw. 42(4): 30:1-30:23, 2016).
19 #include "util/random.h"
22 #include "base/cvc4_assert.h"
26 uint64_t Random::rand()
28 /* xorshift* generator (see S. Vigna, An experimental exploration of
29 * Marsaglia's xorshift generators, scrambled. ACM Trans. Math. Softw.
30 * 42(4): 30:1-30:23, 2016). */
31 d_state
^= d_state
>> 12;
32 d_state
^= d_state
<< 25;
33 d_state
^= d_state
>> 27;
34 d_state
*= uint64_t{2685821657736338717};
38 uint64_t Random::pick(uint64_t from
, uint64_t to
)
41 Assert(to
< UINT64_MAX
);
42 return (Random::rand() % (to
- from
+ 1)) + from
;
45 double Random::pickDouble(double from
, double to
)
48 Assert(to
<= DBL_MAX
);
49 return Random::rand() * (to
- from
) + from
;
52 bool Random::pickWithProb(double probability
)
54 Assert(probability
<= 1);
55 uint64_t p
= (uint64_t) (probability
* 1000);
56 uint64_t r
= pick(0, 999);