Random slot algorithm

后端 未结 4 1509
执念已碎
执念已碎 2020-11-27 08:35

I have two dimensional array. I want to pick a slot at random, and continue to do so never picking the same slot twice until I have finally picked all slots (so nothing ran

4条回答
  •  悲哀的现实
    2020-11-27 09:10

    Assuming your array is like this:

    Random rand = new Random();
    
    object[,] array = new object[width,height];
    bool[,] chosen = new bool[width,height];
    
    int i, j;
    do
    {
        i = rand.Next(width);
        j = rand.Next(height);
    } while (chosen[i,j]);
    
    chosen[i,j] = true;
    object current = array[i,j];
    

    This should work fine.

提交回复
热议问题