Skip to content

Instantly share code, notes, and snippets.

@resilience-me
Last active November 8, 2018 17:17
Show Gist options
  • Save resilience-me/3888f191fdf742f9e7aa3cf5997afc7a to your computer and use it in GitHub Desktop.
Save resilience-me/3888f191fdf742f9e7aa3cf5997afc7a to your computer and use it in GitHub Desktop.
// The shuffle algorithm gets the position of an object from shufflingIndex, using a random number generator,
// and then updates the index so that the same position cannot be given to another object
mapping(uint => uint[2]) shufflingIndex;
uint totalSorted;
mapping(address => uint) pseudonymID;
mapping(uint => address) pseudonymIndex;
function sortMe() atTime(State.interlude, State.pseudonymEvent) {
require(pseudonymID[msg.sender] == 0);
totalSorted++;
pseudonymID[msg.sender] = totalSorted;
pseudonymIndex[totalSorted] = msg.sender;
entropy = generateRandomNumber(entropy);
// Map even and uneven numbers in separate indices, each integer representing a pair with two people
// this lets people who opt-in "hitch-hike" on the shuffling of the entire population, so that optIn()
// can be invoked continuously
if(totalSorted % 2 == 0) {
uint randomNumber = 1 + (entropy % totalSorted/2);
shufflingIndex[randomNumber][1] = totalSorted/2;
shufflingIndex[totalSorted/2][1] = randomNumber;
}
if(totalSorted % 2 == 1) {
uint randomNumber = 1 + (entropy % (totalSorted+1)/2);
shufflingIndex[randomNumber][0] = (totalSorted+1)/2;
shufflingIndex[(totalSorted+1)/2][0] = randomNumber;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment