- Home /
Mystery: Unexpected behavior with randoms and arrays
I'm working with an array of arrays to fill a grid (with actual size CurrentGridSize) with objects. I use randoms to determine the position the objects should take in the array and thus on the grid. The current expected behavior is that there will be around (CurrentGridSize x 4) objects in the array (barring doubles). However, the actual behavior is: the amount of objects is always exactly (CurrentGridSize). And I just can't figure out why that is. Even if I execute the loop 1000 times, the behavior stays the same. Can someone help me out and point out where the problem might be?
Much thanks to whoever can solve this mystery!
for(var k=0;k<(CurrentGridSize*4);++k) {
//calculate randoms for placement on grid
HorIndex = (parseInt(Random.Range(0,CurrentGridSize)));
VertIndex = (parseInt(Random.Range(0,CurrentGridSize)));
//if statement to prevent doubles
if(CompleteGrid[HorIndex][VertIndex] == 0)
{
//instantiate and move to correct position
CompleteGrid[HorIndex][VertIndex] = Instantiate(IslandType);
CompleteGrid[HorIndex][VertIndex].transform.position.x = HorIndex*SquareSize;
CompleteGrid[HorIndex][VertIndex].transform.position.z = VertIndex*SquareSize;
}
}
EDIT I've discovered where the problem might be: if I leave out the if() statement to check if the element is zero, it does work correctly. But now I still don't know why this is the case...
Answer by Ashkan_gc · Jan 24, 2010 at 03:28 PM
the problem is when your new random number is a full array element, you should subtract k by one to run another iteration to find an empty element. 1/4 of random numbers are full elements in your current code so the object will not be instantiated. also you can define a label to go back to it with goto statement when the array element is not equal to zero.
for(var k=0;k<(CurrentGridSize*4);++k) {
//calculate randoms for placement on grid
HorIndex = (parseInt(Random.Range(0,CurrentGridSize)));
VertIndex = (parseInt(Random.Range(0,CurrentGridSize)));
//if statement to prevent doubles
if(CompleteGrid[HorIndex][VertIndex] == 0)
{
//instantiate and move to correct position
CompleteGrid[HorIndex][VertIndex] = Instantiate(IslandType);
CompleteGrid[HorIndex][VertIndex].transform.position.x = HorIndex*SquareSize;
CompleteGrid[HorIndex][VertIndex].transform.position.z = VertIndex*SquareSize;
}
else
{
k--;
}
}
Sorry, that's not it either. The point of the CurrentGridSize is to not exceed that size! It's an array of arrays: the first deter$$anonymous$$es horizontal postion, the second vertical. That being said, there are actually CurrentGridSize available positions in the array. if I let the random exceed this by saying (CurrentGridSize*4) using the randoms, that still doesn't change the weird behavior and it defeats the whole point of having a set size for the grid.
i edited the question. i think i found the answer. the problem is you skip the iteration that could not instantiate a new object in an empty element.
Answer by Lucas Meijer 1 · Jan 24, 2010 at 03:01 PM
It looks like you actually create 4xCurrentGridSize objects, but it looks quitely likely that you move some of them to exactly the same position, making it look like there's only one. Check your hierarchy for the actual amount of islands created.
alas, I already thought of that. The code actually creates CurrentGridSize objects, that's how many there are present. Creating another one at the same position is also prevented in the code (all the elements in the array are initialized at 0, if an element isn't zero, it's taken and can't be taken again).
How exactly are you deter$$anonymous$$ing the number of objects created? (just to make sure there's no error there!)
I checked the Hierarchy panel and the Scene to cross reference and make sure. Anyway, I still haven't found the solution to this riddle but I've written a workaround that does the trick just as well. I still hope to figure it out one day and I'll let you know!