Random number generator for Decentralized Number Guessing Game

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:

  • The user (or an automated system) needs to call back the smart contract to check the result of the game. This is because you cannot generate immediately random values at execution of a smart contract. Indeed, if you could, the block proposer could just systematically win the game. This limitation exists with all the blockchains (not just Algorand).
  • Depending on the security you want to achieve, the wait time to know the result may vary. The lowest security level would consist in using round r+1, which would generally give the result by round r+10.
  • You cannot easily do (on Algorand or any blockchain I am aware of) a guessing game where the user can try multiple guesses in a sequence (and the smart contract says “lower”/“higher”). This is because if the smart contract knows the secret value, then the value is public! There are cryptographic techniques that may allow working with secret values but this is a completely different ball game.

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!