I need your help with a problem. I am using ERC721SeaDrop smart contract provided by opensea. But it mints the token with a token id like 1,2,3 etc. I want it to generate random token ID for NFTs. So how can I achieve this?

1 Answer 1


To generate a random token ID for ERC721 tokens, you can use the keccak256 hash function to generate a hash of a random number, which can then be used as the token ID. Here's an example of how to do this in Solidity:

function generateRandomTokenId() internal returns (uint256) {
  uint256 randomNumber = uint256(keccak256(abi.encodePacked(block.timestamp, block.difficulty, msg.sender)));
  return randomNumber % (10 ** 15); // limit the maximum token ID to 15 digits

Note that generating a random token ID can potentially cause collisions with existing token IDs, which can lead to errors when trying to transfer or trade the NFT. To avoid this, you can implement a checking mechanism to ensure that the generated token ID is not already in use.

  • This wont work with seadrop smart contract Commented May 23, 2023 at 9:30

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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