0

For reference, this question is basically the same as this one, but for solidity.

Of course that in theory you can generate a random, unique, non-duplicate number using solidity. The problem is most solutions to this issue involve knowing beforehand all the possible values before running a randomizing code over them, and for thousands and thousands of different possibilities, storing that on-chain in the contract, that would probably end up being quite expensive gas-wise.

If I want to generate a unique, non-duplicate, random number in a solidity contract, can I do so without actually storing ALL of the possible outcomes on the contract itself? Is it possible to retrieve that data from offchain? Or maybe store it on-chain and retrieve from onchain, but do it in a cheap way?

Thanks.

2 Answers 2

1

In practice, it's really difficult to generate a psuedo-random number in Solidity without compromising on security. In most cases, you would have to use a randomness oracle service. And for that, I would strongly suggest you look into Chainlink VRF. It has quickly become an industry-standard for randomness.

Here's an excerpt from the docs:

Chainlink VRF generates a random number and cryptographic proof of how that number was determined. The proof is published and verified on-chain before it can be used by any consuming applications. This process ensures that the results cannot be tampered with nor manipulated by anyone, including oracle operators, miners, users and even smart contract developers.

0

Of course that in theory you can generate a random

You cannot generate random numbers in any blockchain that is deterministic. Because derministicity implies that nothing can be random.

Is it possible to retrieve that data from offchain?

You need to rely off-chain oracles for random numbers.

Or maybe store it on-chain and retrieve from onchain, but do it in a cheap way?

Not possible.

2
  • you can generate pseudorandom number in solidity. if a cosine function is possible to implement, then a random number is possible too. But it is not going to be very useful on the long run since miners are going to hack his algorithm
    – Nulik
    Aug 18, 2021 at 19:21
  • actually, I implemented a random number already, and depending on the random number I hacked some of the PGA (Priority Gas Auction) bots , who bought into my lotery game. But I am not going to disclose how I did it, top secret
    – Nulik
    Aug 18, 2021 at 19:24

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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