Key Lol
Key Lol

Reputation: 85

How to generate random words in solidity based on a string of letters

I would like to make a word generation function based on the letters of the alphabet, but I don't know how to proceed. I would like to generate 12 words

string letters public = "abcdefghijklmnopqrstuvwxyz"

But I don't understand how I could proceed in solidity to generate

For numbers, I do this

pragma solidity ^0.8.0;
contract RandomNumbers{
    function random(uint number) public view returns(uint){
        return uint(keccak256(abi.encodePacked(block.timestamp,block.difficulty,  
        msg.sender))) % number;
    }
}   

But for words, I don't know how I could proceed

Upvotes: 1

Views: 1176

Answers (2)

Jewelson Noronha
Jewelson Noronha

Reputation: 71

I just improved the answer of @Yilmaz to make the functions view only from payable.

There is an additional function called roll() which outputs true or false based on chance. The 101 within roll() can be altered to 10001 to make the odds down to 0.01% or you can make it dynamic by passing a parameter in place of 101.

// SPDX-License-Identifier: MIT
pragma solidity ^0.8.9;

contract Random {

    //Roll Chance

    function roll(uint256 number) public view returns (bool) {
        uint256 d = uint256(
            keccak256(abi.encodePacked(block.difficulty, block.timestamp))
        ) % 101;
        if (d <= number) return true;
        return false;
    }

    //Random Number Generator
    
    function random(uint256 number, uint256 counter)
        public
        view
        returns (uint256)
    {
        return
            uint256(
                keccak256(
                    abi.encodePacked(
                        block.timestamp,
                        block.difficulty,
                        msg.sender,
                        counter
                    )
                )
            ) % number;
    }

    //Random String Generator (Max length 14)

    function randomString(uint256 length) public view returns (string memory) {
        require(length <= 14, "Length cannot be greater than 14");
        require(length >= 1, "Length cannot be Zero");
        bytes memory randomWord = new bytes(length);
        // since we have 62 Characters
        bytes memory chars = new bytes(62);
        chars = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
        for (uint256 i = 0; i < length; i++) {
            uint256 randomNumber = random(62, i);
            // Index access for string is not possible
            randomWord[i] = chars[randomNumber];
        }
        return string(randomWord);
    }
}

I hope this Helps.

Upvotes: 1

Yilmaz
Yilmaz

Reputation: 49291

contract MiniTest {

    string public letters = "abcdefghijklmnopqrstuvwxyz";
    // I needed to add this to the random function to generate a different random number
    uint counter =1;

    // size is length of word
    function randomString(uint size) public  payable returns(string memory){
        bytes memory randomWord=new bytes(size);
        // since we have 26 letters
        bytes memory chars = new bytes(26);
        chars="abcdefghijklmnopqrstuvwxyz";
        for (uint i=0;i<size;i++){
            uint randomNumber=random(26);
            // Index access for string is not possible
            randomWord[i]=chars[randomNumber];
        }
        return string(randomWord);
    }

    function random(uint number) public payable returns(uint){
        counter++;
        return uint(keccak256(abi.encodePacked(block.timestamp,block.difficulty,  
        msg.sender,counter))) % number;
    }
}
  • I slightly changed the random function because it was generating the same random number so the result for 5 characters was like "jjjjj".

  • string in solidity has no index property. so we have to work with bytes and then convert it to string

Here is the proof of work:

enter image description here

Upvotes: 1

Related Questions