* Return a random number in the range 0..(n-1). */
| 112 | * Return a random number in the range 0..(n-1). |
| 113 | */ |
| 114 | unsigned int |
| 115 | DefaultRndNumGenerator::rnd_upto(const unsigned int n, const Filter *f, const std::string *where) |
| 116 | { |
| 117 | static int g = 0; |
| 118 | int h = g; |
| 119 | if (h == 440) |
| 120 | BREAK_NOP; // for debugging |
| 121 | unsigned int v = genrand() % n; |
| 122 | unsigned INT64 local_depth = rand_depth_; |
| 123 | rand_depth_++; |
| 124 | //ofstream out("rnd.log", ios_base::app); |
| 125 | //out << g++ << ": " << v << "(" << n << ")" << endl; |
| 126 | |
| 127 | if (f) { |
| 128 | while (f->filter(v)) { |
| 129 | // We could add numbers into sequence inside the previous filter. |
| 130 | // If the previous filter failed, we need to roll back the rand_depth_ here. |
| 131 | // This will also overwrite the value added in the map. |
| 132 | rand_depth_ = local_depth+1; |
| 133 | v = genrand() % n; |
| 134 | /*out << g++ << ": " << v << "(" << n << ")" << endl;*/ |
| 135 | } |
| 136 | } |
| 137 | //out.close(); |
| 138 | if (where) { |
| 139 | std::ostringstream ss; |
| 140 | ss << *where << "->"; |
| 141 | trace_string_ += ss.str(); |
| 142 | } |
| 143 | add_number(v, n, local_depth); |
| 144 | return v; |
| 145 | } |
| 146 | |
| 147 | /* |
| 148 | * Return `true' p% of the time. |