I am looking for the less gas consuming random number generator. I think i need an oracle but can you direct me to the best ?


  • You might want to include what the random number generator will be used for... if the random number is not so sensitive, there are ways to generate pseudo-randomness for basically free on the blockchain. Sep 17, 2018 at 20:39
  • It is for a loterry. I think a pseudorandom is always dangerous. But maybe a precise idea ?
    – Nova Lova
    Sep 17, 2018 at 21:08
  • 1
    Yes, for a lottery you definitely want to use an oracle. I only have experience with oraclize.it, so I will let others with more experience answer your question, but my understanding is that using "authenticity proofs", you and others can be certain that the random numbers generated are indeed generated by an independent 3rd party. Sep 17, 2018 at 21:11

2 Answers 2


You can use a Chainlink VRF

You can see this answer on a similar post which also has the exact code to implement it. You are correct to assuming you need an oracle, as the blockchain is deterministic so any number created on it is only pseudo-random, which can lead to attacks.

Chainlink VRFs are a provably random implementation of a random number oracle. The next step would be to run this in a decentralized manner as well (in development).


To generate a really good random number, you could use block hashes of future blocks. But that would require you to do two contracts call:

  • first contract call to "agree" which future block to take into account.
  • the second contract to actually use that block, which now is from the past.

An short example:

pragma solidity ^0.4.25;

contract RandomNumberReservation {
    uint public reservedBlockNumber;

    constructor(uint futureNthBlock) public {
        reservedBlockNumber = block.number + futureNthBlock;

    function getCurrentBlockNumber() public view returns (uint) {
        return block.number;

    function getRandomNumber() public view returns (bytes32) {
        require(block.number > reservedBlockNumber, "Time has not come yet");
        return blockhash(reservedBlockNumber);
  • Oh thanks Miao. This method don't consume too much gas ?
    – Nova Lova
    Sep 17, 2018 at 22:01
  • Each reservation would require a transaction. And after that you could retrieve the random number when Although you could write better version of that to save the amount of gas used for each transaction. I don't know how much is too much gas for you, but at least it wont' require you to use some external oracle services. Sep 17, 2018 at 22:06

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.