Skip to content

Instantly share code, notes, and snippets.

@j05u3
Last active November 7, 2022 03:07
Show Gist options
  • Save j05u3/b8d8da29662254b20d758559a584a66e to your computer and use it in GitHub Desktop.
Save j05u3/b8d8da29662254b20d758559a584a66e to your computer and use it in GitHub Desktop.
// fisher yates:
mapping(uint256 => uint256) private _fyAlreadySeen; // defaults to zero, so stores indexes from 1
function fyGetMapping(uint256 ridx) private view returns (uint256) {
uint256 m = _fyAlreadySeen[ridx];
if (m != 0) return m - 1;
return ridx;
}
function fySetMapping(uint256 ridx, uint256 idx) private {
_fyAlreadySeen[ridx] = idx + 1;
}
function mintOneNft() private {
// getting a random number in the interval [totalSupply(); maxNfts>
// where totalSupply() is the current amount of NFTs already minted
uint256 ridx = _random(totalSupply(), maxNfts, totalSupply());
uint256 tokenId = fyGetMapping(ridx);
uint256 oldTokenId = fyGetMapping(totalSupply());
fySetMapping(ridx, oldTokenId);
_safeMint(msg.sender, tokenId);
}
@j05u3
Copy link
Author

j05u3 commented Jul 18, 2022

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment