How to Random Number Generator in Solidity using keccak256

Share This Post

Wow On more time we found a good topic for my users that is Random Number Generator in Solidity using keccak256 this is our today topic, I’m sure that you know about the basic info.

Random Number Generator in Solidity using keccak256

The random numbers are numbers that occur in sequence, Here we will cover this with the following two mandatory conditions:

  • Anyone cann’t predict future values based on past outputs
  • The values distribute after every miny second

So, let’s go and see the code below and if you want to create your own random number then follow below example:

First of all Convert that hash to an uint, and then use % 100 if you want to take only the last 2 digits. This will give us a totally random number between 0 and 99.

For Example:

// Solidity program to
// demonstrate on how
// to generate a random number
pragma solidity ^0.6.6;
// Creating a contract
contract WholeblogsRandom
// Initializing the state variable
uint randNonce = 0;
// Defining a function to generate
// a random number
function randMod(uint _modulus) internal returns(uint)
   // increase nonce
   return uint(keccak256(abi.encodePacked(now,
                                          randNonce))) % _
Random number generator solidity keccak256
                                                    Random num generator solidity keccak256

Read More: Use msg.sender in Solidity Code


Related Posts

5 Reasons That Make MacBooks Good for Coding

Looking for a reliable laptop or computer for coding?...

Python for Loop Iteration: How to Easily Manipulate a List

One of the most powerful features of the Python...

How To Get First Characters of a String in Python

Way to find the first characters of a string...

How To Convert Tuple To String In Python

In this post, you will learn how to convert...

How To Convert String to Double in Python

Good to hear, that you want to convert a...

Python TypeError: String Index Out Of Range Solution

A string is an array of characters and part...
- Advertisement -spot_img