Skip to content

Instantly share code, notes, and snippets.

@joselvelez
Created November 21, 2021 20:21
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save joselvelez/782c3dcf09964f655ee5e60ed5a44906 to your computer and use it in GitHub Desktop.
Save joselvelez/782c3dcf09964f655ee5e60ed5a44906 to your computer and use it in GitHub Desktop.
Solidity pseudo random generator, for testing purposes
contract PseudoRandom {
function abiEncode(string memory stringInput) public view returns (bytes memory) {
return abi.encode(stringInput, msg.sender, block.timestamp);
}
function kHash(bytes memory bytesInput) public pure returns (uint) {
return uint(keccak256(bytesInput));
}
function random(string memory input, uint arrayLength) public view returns (uint) {
bytes memory abiEncodeOutput = abiEncode(input);
uint kHashOutput = kHash(abiEncodeOutput);
return kHashOutput % arrayLength;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment