uint private seed;
// starts from 0 (including) to _length number (excluding)
function random(uint _length) private returns(uint) {
seed = seed.add(block.timestamp.add(uint(msg.sender)));
return uint(sha256(toBytes(uint(blockhash(block.number - 1)) + seed))) % _length;
}
function toBytes(uint256 x) private pure returns(bytes b) {
b = new bytes(32);
assembly {
mstore(add(b, 32), x)
}
}
How safe is this to be used for generating a random number?
I have a seed
in there based on the previous generated random number which would make it pretty much impossible to track.
will this be expensive in terms of gas usage?
seed
, it's just based the previous generated number which that previous number is also based on it previous and so on. enlighten me if i'm missing something here