I am making a flash site that has an image in the background and theres a function that chooses a random number from 0 to the number of images-1 and makes the background that image. This function works, but I wanted to make it so it would not repeat an image until all have been shown. For some reason the function will randomly return a NaN and I cannot figure out why.

This is my function that changes the image: imageVector is a vector that has all my images in it.



function getRandom(old:Number) {
            var num:Number = Math.ceil(Math.random()*(imagesVector.length-1));
            var alreadyNum:Boolean = false;
            for(var i:Number = 0; i < numberHolder.length; i++) {
                if(num == numberHolder[i])
                    alreadyNum = true;
            }
            if (alreadyNum) {
                getRandom(old);
            }
            else {
                numberHolder.push(num);
                return num;
            }
        }

For some reason, randomly, the function will return a NaN. I tried making an if statement where if num was a NaN just call getRandom again but I get the error Error: Error #1023: Stack overflow occurred. when num becomes NaN.

  • NOTE: If I remove the for statement the problem does not happen, so its the for that is causing the problem. Stephen Cronin almost 9 years ago

3 answers

0
points

If this is overflowing the stack then that probably means that your recursive call to getRandom is of the infinite variety and you either need to do some better police work or stop using recursion altogether.

I don't use Flash, but surely there must be a debugger you can step through this with and see where your logic is faulty.

Answered almost 9 years ago by Nathan Duran
0
points

The flash debugger is not that great with certain errors like these. I know that the reason I am getting a stack overflow is because the loop is infinite. The only reason I can think of of why that happens is num keeps getting set to NaN which I dont get.

Answered almost 9 years ago by Stephen Cronin
  • If it will let you set a breakpoint on the recursive call then that's all the greatness you need. You are more than likely getting an invalid array index at some point and need to correct your math. Also, you don't generally post new answers to your own question when they aren't actually answers. Nathan Duran almost 9 years ago
0
points

I've solved the problem. Instead of having it call getRandom() if a duplicate is chosen I have to have it return getRandom() Full code:


function getRandom() {
            num = Math.ceil(Math.random()*(imagesVector.length-1));
            alreadyNum = false;
            for(var i:Number = 0; i < numberHolder.length; i++) {
                if(num == numberHolder[i]) {
                    alreadyNum = true;
                }
            }
            trace("function: "+num);
            if (alreadyNum) {
                return getRandom();
            }
            else {
                numberHolder.push(num);
                return num;
            }
        }
    
Answered almost 9 years ago by Stephen Cronin