-
Notifications
You must be signed in to change notification settings - Fork 0
/
Random.sol
78 lines (63 loc) · 2.32 KB
/
Random.sol
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
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
pragma solidity ^0.4.23;
contract Random {
function random(address callee, uint salt) internal view returns(uint) {
return uint(keccak256(abi.encodePacked(blockhash(block.number), now, callee, salt)));
}
// accepts already generated random number and limit it to upper value
function random(uint rnd, uint upper) internal pure returns (uint) {
if (upper == 0) {
return 0;
}
return rnd % upper;
}
function random(uint upper, address callee, uint salt) internal view returns(uint) {
if (upper == 0) {
return 0;
}
return random(callee, salt) % upper;
}
function random(uint min, uint max, address callee, uint salt) internal view returns (uint) {
require(min < max);
return min + random(max - min, callee, salt);
}
function random(uint rnd, uint rangeSize, uint exp) internal pure returns(uint) {
uint d = 10**exp;
if (rangeSize == 0 || d == 0) return 0;
return rnd % (rangeSize * d) / d;
}
function random(uint rnd, uint upper, uint rangeSize, uint exp) internal pure returns (uint) {
if (upper == 0) {
return 0;
}
return random(rnd, rangeSize, exp) % upper;
}
function random(uint rnd, uint min, uint max, uint rangeSize, uint exp) internal pure returns (uint) {
if (min >= max) return 0;
return min + random(rnd, rangeSize, exp) % (max - min);
}
// pick or generate new number in [0..1 000 000)
function extract(uint rnd, uint min, uint max, uint rangeSize, uint exp) internal pure returns(uint, uint, uint) {
if (exp == 71) {
// generate new random based on current
rnd = uint(keccak256(rnd));
exp = 0;
}
return (min + random(rnd, rangeSize, exp) % (max - min), rnd, exp+1);
}
function getRandomInRange(uint rnd, uint min, uint max) internal pure returns(uint) {
if (max <= min) {
return min;
}
return min + rnd % (max - min);
}
function getRandom(uint seed) internal pure returns(uint) {
return uint(keccak256(seed));
}
function getRandom(uint seed, uint salt) internal pure returns(uint) {
return uint(keccak256(abi.encodePacked(seed, salt)));
}
function getRandom(uint seed, uint salt, uint divider) internal pure returns(uint, uint) {
uint rnd = uint(keccak256(abi.encodePacked(seed, salt)));
return (rnd, rnd % divider);
}
}