IR2Vec
Loading...
Searching...
No Matches
Random.h
1#ifndef RANDOM_H
2#define RANDOM_H
3#include "Setting.h"
4#include <cstdlib>
5
6// the random seeds for all threads.
7unsigned long long *next_random;
8
9// reset the random seeds for all threads
10extern "C" void randReset() {
11 next_random =
12 (unsigned long long *)calloc(workThreads, sizeof(unsigned long long));
13 for (INT i = 0; i < workThreads; i++)
14 next_random[i] = rand();
15}
16
17// get a random interger for the id-th thread with the corresponding random
18// seed.
19unsigned long long randd(INT id) {
20 next_random[id] = next_random[id] * (unsigned long long)(25214903917) + 11;
21 return next_random[id];
22}
23
24// get a random interger from the range [0,x) for the id-th thread.
25INT rand_max(INT id, INT x) {
26 INT res = randd(id) % x;
27 while (res < 0)
28 res += x;
29 return res;
30}
31
32// get a random interger from the range [a,b) for the id-th thread.
33INT rand(INT a, INT b) { return (rand() % (b - a)) + a; }
34#endif