yosys-master
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros
Rnd.h
Go to the documentation of this file.
1 /*******************************************************************************************[Rnd.h]
2 Copyright (c) 2012, Niklas Sorensson
3 Permission is hereby granted, free of charge, to any person obtaining a copy of this software and
4 associated documentation files (the "Software"), to deal in the Software without restriction,
5 including without limitation the rights to use, copy, modify, merge, publish, distribute,
6 sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is
7 furnished to do so, subject to the following conditions:
8 
9 The above copyright notice and this permission notice shall be included in all copies or
10 substantial portions of the Software.
11 
12 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT
13 NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
14 NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
15 DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT
16 OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
17 **************************************************************************************************/
18 
19 #ifndef Minisat_Rnd_h
20 #define Minisat_Rnd_h
21 
22 #include "Vec.h"
23 
24 namespace Minisat {
25 
26 // Generate a random double:
27 static inline double drand(double& seed)
28 {
29  seed *= 1389796;
30  int q = (int)(seed / 2147483647);
31  seed -= (double)q * 2147483647;
32  return seed / 2147483647;
33 }
34 
35 
36 // Generate a random integer:
37 static inline int irand(double& seed, int size) { return (int)(drand(seed) * size); }
38 
39 
40 // Randomly shuffle the contents of a vector:
41 template<class T>
42 static void randomShuffle(double& seed, vec<T>& xs)
43 {
44  for (int i = 0; i < xs.size(); i++){
45  int pick = i + irand(seed, xs.size() - i);
46  T tmp = xs[i];
47  xs[i] = xs[pick];
48  xs[pick] = tmp;
49  }
50 }
51 
52 // Randomly shuffle a vector of a vector (ugly)
53 template<class T>
54 static void randomShuffle(double& seed, vec<vec<T> >& xs)
55 {
56  for (int i = 0; i < xs.size(); i++){
57  int pick = i + irand(seed, xs.size() - i);
58  vec<T> tmp; xs[i].moveTo(tmp);
59  xs[pick].moveTo(xs[i]);
60  tmp.moveTo(xs[pick]);
61  }
62 }
63 
64 
65 //=================================================================================================
66 } // namespace Minisat
67 #endif
Size size(void) const
Definition: Vec.h:64
static void randomShuffle(double &seed, vec< T > &xs)
Definition: Rnd.h:42
static int irand(double &seed, int size)
Definition: Rnd.h:37
void moveTo(vec< T > &dest)
Definition: Vec.h:93
static double drand(double &seed)
Definition: Rnd.h:27