Random Number Generation on Blockchain

Shivam Prajapati
GAMMASTACK
Published in
3 min readFeb 26, 2020

What are Random numbers?

Think of it as random noise or anything random happening around you. The probability of detecting these patterns around is none. That’s what random number is all about, a non-deterministic pattern to generate any number within a given range.

When I say, think of a number between 1–100. Since the number is non-deterministic in nature, I can’t guess the number in your head.

A random number is a number chosen as if by chance from some specified distribution such that a selection of a large set of these numbers reproduces the underlying distribution. Almost always, such numbers are also required to be independent, so that there are no correlations between successive numbers. Computer-generated random numbers are sometimes called pseudo-random numbers, while the term “random” is reserved for the output of unpredictable physical processes. When used without qualification, the word “random” usually means “random with a uniform distribution.” Other distributions are of course possible. For example, the Box-Muller transformation allows pairs of uniform random numbers to be transformed to corresponding random numbers having a two-dimensional normal distribution.

What are the Pseudo-Random Numbers?

Pseudo-random numbers are also called Deterministic Random Bit. These numbers are generated by computer algorithms called Pseudo-Random Number Generators (PRNG). They are deterministic and are not truly random numbers, they repeat themselves after a particular sequence.

To generate a random number, we need a number to initiate the algorithm so, we usually take system time and pad it with a number (any number) generally called a SEED to do that, and to ensure that these random numbers stay random we add SALT (It’s like a password for your random number) to the number to avoid the hash collision (If the number generation time is short enough and we are not using a salt or sugar, then chances are same seed will be used to generate two or more numbers hence ending up with same hash and same numbers every time).

PRNGs on the blockchain.

From the above definitions, we know that random numbers are not truly random in computers and also, at least a system time is required to initiate the algorithm which is not good especially if you’re using it for applications like casino games, computer simulation, cryptography, etc. you’ll encounter more hash collisions.

And that is why it is almost impossible to generate a random number on the blockchain. Because we somehow need to get the time or something else like block hash or etc. to initiate the random number generation, and in first and second-generation blockchain block mining time was very high and if you generate random numbers on these chains chances are you’ll get hash collision more then ever.

But, because of the very fast block mining time (0.5 — 1s) in third-generation blockchains, we can utilize the block time or block-hash to initiate the PRNG.

I have written a little smart contract in solidity to demonstrate how we can do that on the blockchain.

You can say, it’s the true random number generator (TRNG) because we’re not using an algorithm to generate the random number we’re just hashing the block timestamp with our seed, salt, and sugar. These three values chosen by us could be the same and deterministic but the block hash is non-deterministic. And it works just fine for Ethereum and TRON and any other third-generation blockchain which supports solidity.

In the above contract, I have written three functions one with only seed, one with seed and salt, and one with seed, salt, and sugar.

abi.encodePacked packs the bytes of the data tightly without any padding, so it’s impossible to extract the data from this function if it has no padding. Type uint256 can be extracted from that function. It’s return type is bytes.

keccak256 is the hashing function same as the sha3 but with different padding patterns. For more information please read this.

So, in the above functions, I’m actually encoding the data which included time, seed, salt, and sugar to more randomness and make the random number as unpredictable as possible.

Surely, we can delete timestamp from the above functions. But in that case, we’ll have full control over the random number generation thereby compromising the blockchain’s transparency.

--

--