I'm an beginner in solidity, and with a teamate we have a project for an NFT contract.

I have a contract with 5000 NFT limite (cards, and 20 specific card form a familily, each family and cards are unique). All this NFT card are unique by their number of index.

The goal of the game its to assemble a familily of card (example : card number 1 to 25 form the famili RED) for win a prize.

So, in the start of the game, we need to distrib this card randomly. In my function "claimCard", a "random number" is calculate, and the number's index of the cards is attribute to the owner address.

I Have :

 function claimCard(uint _cardIndex) public payable returns(uint){
 _cardIndex = 5000;

cardIndexToAddress[uint(keccak256(abi.encodePacked(now, msg.sender, randNonce))) % _cardIndex] = msg.sender;

 return uint(keccak256(abi.encodePacked(now, msg.sender, randNonce))) % _cardIndex;

Problem : in case with the "random number" is a card that already owned, how can I manage my function for re start and find a new random number for a card not already owned ?

  • I can take a loop (if address card is != 0x0, loop) but this solution can cost a big amount of gas 50% of card claim => 2loop 33% of card claim => 3loop 10% of card claim => 10loop 4% of card claim => 25loop 1% of card claim => 100loop

  • I can create an array with 5000 index, like arrayCard(1, 2, 3... 5000) and with a random number modulo lengh.array, take the number and remove the element in the array, so the array become with 5000 element - 1... But the cost of an array of 5000 element its too expensive...

Have u got some issue for me ? :) Thanx you and have a nice day !

2 Answers 2


Create the cards array containing random numbers (466,35,77,2466,....) in the NFT's constructor or some initialize method. When rewarding the card just use the first element from the cards array then remove it. Still not sure how much gas would consume such a deploy or initialize of the NFT smart contract

  • 1
    Yes I can create an array cards with 5000 elements. But the cost of gas to array.push 5000 elements is to 100millions of gas... So 13 ether cost with 127 gwei network... Plus 25 millions of gas for delete each element... It's not a good issue for me :/ The way for a normal cost of transaction for each claimCards is already in search.
    – wardesqwe
    Feb 25, 2021 at 20:58

For the curious, here is how I treated my problem :

For a pseudo-random distribution of my 200 family, I separate the claim on two way :

  • The first way is a pseudo-random number with keccak function on my post. This is the way for 4500 token. When assignToken > 4500, the function go to a second party
  • The second way is to send the next claim to the token number 1. If it's claim, the next is 2, and etc... No randomNumber here, but the goal is to save gas for user.

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.