The Dumbest Bug I’ve Ever Created

I was testing stuff yesterday… and the bug I created was really dumb.

So stupid that I just had to write about it here.

Basically my code was something like this (I’ve used pseudo code here to simplify the situation). I have a code that loops 10 times and sets random texture to a character entity:


FOR (i=0; i<10; i++)
{
SRAND(time)
value = RAND(1 - 20)
textureName = "texture" + value + ".jpg"
entity[i]->setTexture(textureName)
}

When I launched the program, for some reason the first 8 entities were getting a same texture, and the last 2 were getting some other texture. I launched it couple of times and thought that is there something wrong with making random numbers.

I decided to debug and placed a breakpoint to the line of “value”, and watched what kind of numbers it was getting. It was getting number 3, then number 9, then number 5 and so on… when I debugged – the textures were random! But the moment I launched without debugging and breakpoints, the random number wasn’t working.

After some time… I realized that I had add that SRAND call inside the loop. Basically I was resetting the random number set every time I started the loop (for those who aren’t familiar with setting seed, basically you could describe it like that the computer will start giving “certain type of random numbers” based on the seed number. The bottom line was close to this: “when next RAND call is done, give me number 3″). I was using that command every time, just before asking the random number.

And that’s why the breakpoint mode gave “proper textures”, since some time had passed between loops (as I was checking the number value). But in the non-debug/non-breakpoint mode the loop went so fast that the “time” value was always the same.

It was easy (yet embarrassing for somebody who has been programming close to two decades ;)) to fix. I simply moved the SRAND to another place and now things started work.

SRAND(time) // initialize random number generating
.
.
.
FOR (i=0; i<10; i++)
{
value = RAND(1 - 20) //give random number between 1-20
textureName = "texture" + value + ".jpg"
entity[i]->setTexture(textureName)
}

Care to share your dumbest programming bug you’ve managed to create?

One thought on “The Dumbest Bug I’ve Ever Created

  1. Pingback: Feed Search Engine - All Fresh Articles And News Are Here

Comments are closed.