Search
 
SCRIPT & CODE EXAMPLE
 
CODE EXAMPLE FOR JAVASCRIPT

get random number in solidity

// 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 by stackoverflow.com #
 
PREVIOUS NEXT
Tagged: #random #number #solidity
ADD COMMENT
Topic
Name
8+6 =