-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathgenetics.sol
More file actions
42 lines (32 loc) · 1.13 KB
/
Copy pathgenetics.sol
File metadata and controls
42 lines (32 loc) · 1.13 KB
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
pragma solidity ^0.4.8; //specify compiler version
contract Genetics {
enum Gene {
string first;
string second;
}
uint256 _seed;
// The upper bound of the number returns is 2^bits - 1
function bitSlice(uint256 n, uint256 bits, uint256 slot) public pure returns(uint256) {
uint256 offset = slot * bits;
// mask is made by shifting left an offset number of times
uint256 mask = uint256((2**bits) - 1) << offset;
// AND n with mask, and trim to max of 5 bits
return uint256((n & mask) >> offset);
}
function maxRandom() public returns (uint256 randomNumber) {
_seed = uint256(keccak256(
_seed,
block.blockhash(block.number - 1),
block.coinbase,
block.difficulty
));
return _seed;
}
// return a pseudo random number between lower and upper bounds
// given the number of previous blocks it should hash.
function random(uint256 upper) public returns (uint256 randomNumber) {
return maxRandom() % upper;
}
function breed(Gene father, Gene mother) public {
}
}