Good day everyone,
I am creating a Decentralized Number Guessing Game , and I want to generate a Random Number from 1 to 100 like this example below:
secret_number = random.randint(1, 100)
Does anyone know how to apply this one to PyTeal?
Good day everyone,
I am creating a Decentralized Number Guessing Game , and I want to generate a Random Number from 1 to 100 like this example below:
secret_number = random.randint(1, 100)
Does anyone know how to apply this one to PyTeal?
To securely generate randomness on Algorand (as for most other chains), you need to use a randomness beacon/oracle.
See Usage and Best Practices for Randomness Beacon | Algorand Developer Portal for one randomness beacon of Algorand.
Note that the smart contract cannot keep the random number secret.
On Algorand, as on all the public blockchains I am aware of, smart contract execution is completely public. All data inside can be seen by everyone.
This is necessary because all the nodes running the blockchain need to execute the smart contract.
(There are potentially cryptographic solutions to avoid that but they are not deployed in any major network to my knowledge.)
Now, this may or may not be an issue depending on your exact game.
If the game is to exactly guess the number: if you did not guess correctly, you fail.
Then, the randomness beacon above works very well.
When the user starts the game, they indicate their guess. The smart contract then records the guess as well as the round number r.
The smart contract implicitly commits to use the randomness beacon for a certain round depending on r, say r + 10 (the 10 is a parameter to choose that depends on the security you want to achieve - the formula can also be improved to align with multiple of 8 for faster response - I can explain more if needed).
When the randomness beacon provides the value for round r+10 (which in general should be before r+21 - but this can be improved using the tweak above), then the user can call back the smart contract to see if they win or not.
A few notes:
Oh I see .
I’ve been currently creating a web app where react js is the frontend (see pic below)
and I am planning to add an algorand smart contracts to it.
I will try the randomness beacon this week
Thanks
I think it may be difficult for your example as you have 10 tries. (Whether on Algorand or any other blockchains, you cannot really hide the number to be guessed in that case.) But let us know what you manage to do!