This question already has an answer here:

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?

marked as duplicate by Ismael, Achala Dissanayake, flygoing, shane, Briomkez Mar 24 at 22:27

This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.

  • Do you mean a truly random string? If that were possible there would be a trivial reduction to random numbers – Tjaden Hess Mar 22 at 17:24
  • no. I mean, a pseudo-random string. – João Quintanilha Mar 22 at 17:25

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


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.