-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrandom.h
43 lines (37 loc) · 1.03 KB
/
random.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
#ifndef SKETCHES_RANDOM_H
#define SKETCHES_RANDOM_H
#include <stdlib.h>
#include <random>
#include "ttmath/ttmath.h"
/******************** Random constructor by type ******************************/
template<typename T>
T random(){
return (T) rand();
};
template<unsigned int length>
ttmath::UInt<length> random_ttmath(){
ttmath::UInt<length> result;
for ( unsigned int i = 0; i<length; i++)
result.table[i] = rand();
return result;
};
template<unsigned int length>
ttmath::UInt<length> random_ttmath(unsigned bits){
ttmath::UInt<length> result;
std::random_device rd;
for ( unsigned int i = 0; i<length; i++){
if ( bits < 64 ) {
std::uniform_int_distribution<uint64_t> dist(0, (1UL<<bits) - 1);
result.table[i] = dist(rd);
bits = 0;
} else {
std::uniform_int_distribution<uint64_t> dist;
result.table[i] = dist(rd);
bits -= 64;
}
}
return result;
};
template<typename T>
T random(unsigned bits);
#endif