THE COMPREHENSIVE GUIDE

How to securely generate random numbers in Solidity?

Random numbers in Solidity using the Niguez Randomity Engine

The Plutocrat
Jun 21, 2019 · 2 min read
Image for post
Image for post

This is a guide to generate Random Numbers in Solidity Using the Niguez Randomity Engine. Learn about Niguez Randomity Engine here:

Import API into your project | Use the following code:

pragma solidity ^0.5.0;/**For Ethereum*/
import
"https://github.com/niguezrandomityengine/ethereumAPI/nreAPI.sol";
/**For ThunderCore*/
import "https://github.com/niguezrandomityengine/thundercoreAPI/nreAPI.sol";

Create a Smart Contract as follow (Ethereum):

pragma solidity ^0.5.0;

import "https://github.com/niguezrandomityengine/ethereumAPI/nreAPI.sol";

contract Randomness is usingNRE {

}

Code function to use sequences and set slots, there are 24 sequences i.e., ra(), rb(), rc() up to rx():

Example 1: Using the explicit sequence to get a 10 digits random number.

pragma solidity ^0.5.0;import "https://github.com/niguezrandomityengine/ethereumAPI/nreAPI.sol";contract Randomness is usingNRE {   function randomNumber() public view returns (uint256){
return (ra()%(10**10));
}

}

Example 2: Using multiple sequences to generate 3 digits random number ranging from 0 to 999. This type of random number generation can be used to randomly select validators in a Proof of Stake (PoS) network.

pragma solidity ^0.5.0;import "https://github.com/niguezrandomityengine/ethereumAPI/nreAPI.sol";contract Randomness is usingNRE {   function randomNumber() public view returns (uint256){
return (((rf()%10)*100)+((rx()%10)*10)+(rm()%10));
}

}

Example 3: Transaction of random numbers into state or local variable. The below transaction will consume gas, approximately (40k — 120k) gas per sequence.

pragma solidity ^0.5.0;import import "https://github.com/niguezrandomityengine/ethereumAPI/nreAPI.sol";contract Randomness is usingNRE {uint256 public randomNumber;event rNum(uint256 theNumber);       /**State Variable. */
function stateRandomNumber() public {
randomNumber = (((ru()%10)*100)+((re()%10)*10)+(rq()%10));
}

/**Local Variable. */
function localRandomNumber() public {
uint256 randomGame = (((rj()%10)*100)+((rg()%10)*10)+(ri()%10));
emit rNum(randomGame);
}
}

Example 4: Using the slot of numbers from between the sequence and hashing it for distinct random sequence

pragma solidity ^0.5.0;import "https://github.com/niguezrandomityengine/ethereumAPI/nreAPI.sol";contract Randomness is usingNRE {   function randomNumber() public view returns (uint256){
return (uint256(keccak256((rw()/(10**20))%(10**12))));
}

}

These are merely 4 instances of generating a pseudo-random number, the user could use this numbers in infinite possibilities by using dynamic slot selections, cross stitching sequences and using them as a salt to hash with user’s address or nonce.

Thank you for reading. Enjoyed it? Please Share it and follow me on Medium.

Links

Niguez Randomity Engine

Source of random numbers on the Ethereum Blockchain using…

Medium is an open platform where 170 million readers come to find insightful and dynamic thinking. Here, expert and undiscovered voices alike dive into the heart of any topic and bring new ideas to the surface. Learn more

Follow the writers, publications, and topics that matter to you, and you’ll see them on your homepage and in your inbox. Explore

If you have a story to tell, knowledge to share, or a perspective to offer — welcome home. It’s easy and free to post your thinking on any topic. Write on Medium

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store