Since all the answers are a bit old, is there a better or more generic way to get a random number on-chain than using Harbinger Oracle? Would it be safe to imagine a community approved & audited contract that provide random values between 0-1 as a tool to help game developpers on Tezos?
2 Answers
There are a few things you can do:
If you're playing a two-player game, you can have each player commit to some bytes of their choice, have them open the commitment, and hash the result to get a source of entropy. If a player doesn't open their commitment, they lose by default. This is simple and works well, but it doesn't extend to more than two players because collusion then becomes possible.
You could also make the commitments timelocks so that it is always possible to open the commitment, but unfortunately, the TIMELOCK instruction currently needs fixing.
You could have a few parties perform DKG (distributed key generation) to hold shards of a BLS private key. This means you can have say, 7 parties holding key shards, and whenever any 4 of them sign the last piece of randomness, you obtain a unique BLS signature that can be your next piece of randomness. So long as there are no more than 3 dishonest parties in the 7, the scheme will efficiently produce random numbers.
You could subscribe to a trusted randomness oracle.
You could accept the header of Bitcoin blocks, check that they pass Bitcoin's proof-of-work difficulty and treat those as random.
-
Thank you Arthur, I will take a closer look at oracles. If the game is an open-world where more than two players can perform actions reliying on randomness at the time of their choosing, it seems the best solution. And thank you for everything else, the more I learn about Tezos the more I love it Nov 12, 2022 at 18:20
The only way to achieve randomness in Tezos smartcontract is to use commit/reveal scheme.
There is an audited template contracts in ligo:
You can initiate a project from any with:
ligo init contract [PROJECT_NAME] -t [TEMPLATE_NAME]
Warning: those contract are an improved version of commit/reveal that use tezos timelock
Timelock will be deactivated from tezos if Lima proposal is deployed, and it will not possible anymore to deploy a contract with a timelock. In that case, we will update contract for classical commit/reveal.
Contracts deployed before Lima will still be usable
Note: there is no float in Michelson, but ligo provides a math lib that gives float support in Ligo if you need it