# How to securely generate random numbers in Solidity?

## Random numbers in Solidity using the Niguez Randomity Engine

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.

Written by

Written by

## The Plutocrat

#### Solidity Developer · Financial Engineer · Graphic Designer 