jeudi 30 août 2018

Please help me verify this parallel Fisher-Yates shuffle algorithm [on hold]

I try to find a way I could make a parallel shuffling algorithm

Suppose I have 100 objects, each kept its own index 0-99. I need to shuffle the index of each one in parallel and I could not making array to shuffle it normally because it might be scaled to million. So it not practical to create million array of million object in parallel

So I just came up with this about counting the number of random to match the index of object

var random = new Random(seed); // random with seed in C#
var obj = new { index : 16 }; // the index of the 16th object
int i = 100; // number of object amount
while(i > 0)
{
    i--; // 99,98,97,... index to put object into

    int rand = random.Next(0,i); // 0-99,0-98,0-97,...
    if(rand == obj.index)
        break;

    // simulate index of array in original Fisher-Yates,
    // if the index that was randomed out was less than all the object, 
    // means that the object in that index was taken out and put to the i index,
    // so every index after that would be shifted by one
    if(rand < obj.index)
        obj.index--; 
}

obj.index = i; // simulate index that put to the last of array

Are there any problem with this code? Is it really work?

And also, I just came up with this, so I wish that there would be any better shuffle algorithm that could run in parallel like this. Are there any?




Aucun commentaire:

Enregistrer un commentaire