Skip to content

Instantly share code, notes, and snippets.

@resilience-me
Last active November 7, 2018 00:09
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save resilience-me/a8a779fb85e0f8a13e296cf59a57bd72 to your computer and use it in GitHub Desktop.
Save resilience-me/a8a779fb85e0f8a13e296cf59a57bd72 to your computer and use it in GitHub Desktop.
struct RandomizationMechanism {
mapping(uint => uint) sortingMechanism;
uint totalSorted;
bytes32 entropy;
}
RandomizationMechanism sortingTool[];
function sortingHat(uint _sortingType, uint _listSize) internal returns (uint) {
RandomizationMechanism storage s = sortingTool[_sortingType];
s.totalSorted++;
uint randomNumber = uint(sha3(block.blockhash(block.number-1), s.entropy, now)) % (_listSize - s.totalSorted);
uint querySorter = s.totalSorted + randomNumber;
// Instantiate sortingMechanism while people sort themselves
if(s.sortingMechanism[querySorter] == 0) s.sortingMechanism[querySorter] = querySorter;
if(sortingMechanism[totalSorted] == 0) s.sortingMechanism[s.totalSorted] = s.totalSorted;
uint randomPosition = s.sortingMechanism[querySorter];
s.sortingMechanism[randomPosition] = s.sortingMechanism[s.totalSorted];
s.entropy = sha3(randomPosition, msg.sender, now);
if(s.totalSorted == _listSize) s.totalSorted = 0;
return randomPosition;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment