4

I understood that solidity can only generate pseudo-random numbers. Although, is there any way of generating a, let's say 10 character, random string?

2
  • Do you mean a truly random string? If that were possible there would be a trivial reduction to random numbers Commented Mar 22, 2019 at 17:24
  • no. I mean, a pseudo-random string. Commented Mar 22, 2019 at 17:25

1 Answer 1

0

If you only want pseudo-random, and you aren't concerned about malicious actors gaming/breaking your system... then most people seem to take one of these approaches:

1) Using a hash of various block information

Edit: I'M NOT ENDORSING THIS. YOU WILL BE GAMED.

For example:

function genRandom() private view returns (uint) {
    uint rand = uint(keccak256(block.blockhash(block.number-1)));
    return uint(rand % (10 ** 20));
}

source: https://gist.github.com/austintgriffith/de01418675238d5292fcc7fe0de5586c#file-etherbots

Ethernaut level 3 shows just how easy this is to exploit...

2) Using a trusted oracle to source a random number for your contract.

Each approach has limitations and constraints already documented extensively.

Context: How can I securely generate a random number in my smart contract?

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