-
-
Save resilience-me/a8a779fb85e0f8a13e296cf59a57bd72 to your computer and use it in GitHub Desktop.
This file contains 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
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