Last active
November 7, 2022 03:07
-
-
Save j05u3/b8d8da29662254b20d758559a584a66e to your computer and use it in GitHub Desktop.
Fisher Yates - Solidity. Find more details on https://josuejulcarima.medium.com/fisher-yates-solidity-snippet-1ce656e63acc
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
More docs: https://josuejulcarima.medium.com/fisher-yates-solidity-snippet-1ce656e63acc