I'm looking for a source of random numbers - doesn't need to be cryptographically secure, just hard to predict in advance but agreed by and visible to all participants. I'm thinking about using the block hash a few blocks up the chain (say 10). Ideally I want something that would be hard to game values on as a validator.

Is that a suitable source? Are there other / better options? Or existing examples in pallets already?

I think I only need 16 bit (though 32 / 64 bit numbers might be more computationally efficient for the modulo arithmetic I'm planning to use it for - maybe).

1 Answer 1


There are a few pallets in FRAME which provide an implementation to the Randomness trait.

The best of which is the BABE Pallet which provides three different levels of randomness:

  1. CurrentBlockRandomness: Randomness produced semi-freshly with each block, but inherits limitations of RandomnessFromTwoEpochsAgo from which it derives.

  2. RandomnessFromOneEpochAgo: Randomness usable by on-chain code that does not depend upon finality and takes action based upon on-chain commitments made during the previous epoch.

  3. RandomnessFromTwoEpochsAgo: Randomness usable by consensus protocols that depend upon finality and take action based upon on-chain commitments made during the epoch before the previous epoch.

If you use the BABE pallet, you can easily integrate randomness into your other pallets by implementing this trait in your Pallet config.

/// The module's configuration trait.
pub trait Config: frame_system::Config {
    /// Something that provides randomness in the runtime.
    type Randomness: Randomness<Self::Hash, Self::BlockNumber>;

And then you can use this to fetch some randomness easily:

let (hash, block_number) = T::Randomness::random(&b"some_seed_phrase"[..]);

Another Pallet which provides this Randomness implementation is the randomness-colletive-flip pallet. However, the strength of this randomness is much weaker than the one provided by BABE.

  • Thanks Shawn. Presumably the BABE options there are deterministic enough that if all the nodes called CurrentBlockRandomness at block 10_000 they'd all get the same value (forks notwithstanding)? And presumably if I'm running on a AURA chain, I can't include the BABE pallet too and access that randomness? Feb 20, 2022 at 20:34
  • 1
    Yes, all of these randomness options are by design deterministic, otherwise they would be pretty useless. AURA does not provide this, and you cannot use both AURA and BABE, so then using the randomness-colletive-flip pallet. If you are building a parachain, you can access the randomness from the relay chain, which would be even better.
    – Shawn Tabrizi
    Feb 20, 2022 at 20:44

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.