Question : get random number in solidity
Answered by : anxious-alligator-bx1nkddr8shy
// Solidity pseudo-random function:
function random() private view returns (uint) { // sha3 and now have been deprecated return uint(keccak256(abi.encodePacked(block.difficulty, block.timestamp, players))); // convert hash to integer // players is an array of entrants
}
// invoke random function in a pickWinner example function
function pickWinner() public { uint index=random()%players.length;
}
Source : https://stackoverflow.com/a/67332959/10261711 | Last Update : Mon, 13 Dec 21
Question : random function in solidity
Answered by : dejan-ilic
function random() private view returns(uint){ uint source = block.difficulty + now; bytes memory source_b = toBytes(source); return uint(keccak256(source_b)) % 100;
}
Source : | Last Update : Wed, 15 Jun 22