x


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...

more ▼

asked Jan 24 '10 at 02:35 PM

era gravatar image

era
48 2 3 9

(comments are locked)
10|3000 characters needed characters left

2 answers: sort voted first

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--;
        }
}
more ▼

answered Jan 24 '10 at 03:28 PM

Ashkan_gc gravatar image

Ashkan_gc
9.3k 33 56 120

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 determines 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.

Jan 24 '10 at 03:43 PM era

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.

Jan 24 '10 at 06:40 PM Ashkan_gc
(comments are locked)
10|3000 characters needed characters left

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.

more ▼

answered Jan 24 '10 at 03:01 PM

Lucas Meijer 1 gravatar image

Lucas Meijer 1 ♦♦
8k 19 43 85

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).

Jan 24 '10 at 03:19 PM era

How exactly are you determining the number of objects created? (just to make sure there's no error there!)

Feb 17 '10 at 12:30 PM duck ♦♦

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!

Mar 19 '10 at 09:01 AM era
(comments are locked)
10|3000 characters needed characters left
Your answer
toggle preview:

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this question

By Email:

Once you sign in you will be able to subscribe for any updates here

By RSS:

Answers

Answers and Comments

Topics:

x5270
x1725
x598

asked: Jan 24 '10 at 02:35 PM

Seen: 936 times

Last Updated: Feb 17 '10 at 12:14 PM