The algorithm for sorting into pairs

in pseudonympairs •  5 years ago  (edited)

When the event begins, people are in index[registry[msg.sender]]/2, floored.

uint[] index;

function register() {
    registry[msg.sender] = index.length;
    uint link;
    if(index.length != 0) link = getRandomNumber() % index.length;
    index.length++;
    index[index.length - 1] = index[link];
    index[link] = index.length-1;
}

the first person to register is placed into index[0]

the next person, into the position person one was at

index[0] = 1 and index[1] = 0

the next person, into either position of the two previous people

one example, index[0] = 2, index[1] = 0 and index[2] = 1

this here is more or less the code to immigrate. it hitchhikes on the shuffling of the pairs.

uint[] index;

uint[] borderPatrol;
uint immigrants;

function register() {
    uint link;
    if(index.length != 0) link = getRandomNumber() % index.length;
    index.length++;
    registry[msg.sender] = index.length;
    index[index.length - 1] = index[link];
    index[link] = index.length-1;
    
    if(index.length%2 == 0) {
        if(immigrants < index.length/2) { 
            borderPatrol.push(index.length/2); 
        }
    }
}

function immigrate() {
    immigrants++;
    if(borderPatrol.length == 0) immigrant[msg.sender] = immigrants;
    else {
        uint link =     getRandomNumber() % borderPatrol.length;
        immigrant[msg.sender] = borderPatrol[link];
        borderPatrol[link] = borderPatrol[borderPatrol.length - 1];
        borderPatrol.length--;
    }
}
Authors get paid when people like you upvote their post.
If you enjoyed what you read here, create your account today and start earning FREE STEEM!