# Random distribution of large amount of NFT

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;
randNonce++;

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 !