Search
 
SCRIPT & CODE EXAMPLE
 

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;
}
Comment

random function in solidity

function random() private view returns(uint){
  uint source = block.difficulty + now;
  bytes memory source_b = toBytes(source);
  return uint(keccak256(source_b)) % 100;	
}
Comment

PREVIOUS NEXT
Code Example
Javascript :: checkbox on click jquery select all 
Javascript :: javascript wait 5 sec 
Javascript :: how to get the first character of a string in javascript 
Javascript :: jqiery call onclick in another element 
Javascript :: am pm to 24 hours converter javascript 
Javascript :: javascript get viewport dimensions 
Javascript :: Error during serialization or deserialization using the JSON JavaScriptSerializer. The length of the string exceeds the value set on the maxJsonLength property. 
Javascript :: js timer loop 
Javascript :: JS get length of an object 
Javascript :: newtonsoft json change property name 
Javascript :: document on click dynamic element 
Javascript :: javascript seconds to min and seconds 
Javascript :: how to create an array of specific length in javascript 
Javascript :: jquery 1 second after page load 
Javascript :: disable click event jquery 
Javascript :: how to reset form after submit using jquery and ajax 
Javascript :: js push if not exists 
Javascript :: javascript count elements in json object 
Javascript :: remove last 3 characters from string javascript 
Javascript :: javascript convert hex color to rgb 
Javascript :: disable autocomplete in react fields 
Javascript :: mongodb find like 
Javascript :: javascript clear style inline property 
Javascript :: js generate id 
Javascript :: yarn add material ui 
Javascript :: change window location javascript 
Javascript :: react native settimeout 
Javascript :: jquery change font color 
Javascript :: Valid Time javascript 
Javascript :: remove first 3 characters from string javascript 
ADD CONTENT
Topic
Content
Source link
Name
7+4 =