Page 1 of 1

math.random fluctuation

Posted: Sun Jun 21, 2015 1:40 pm
by MattyAB
Hey Everyone,

So I'm in the process of writing my game (if you haven't seen it already, check out my post about it here: viewtopic.php?f=5&t=80361), and have come across a problem with all of the columns of blocks appearing together. I put in some debug code with prints, and saw that when it randomises the number, the variable fluctuates between 1 and 2 for a few seconds before settling on 1 number. By this time, all of the columns of blue have spawned in 1 place. Am I calling the math.random function wrong or is there a bug in my code? Attached will be a .love file of the game.

Thanks!

Re: math.random fluctuation

Posted: Sun Jun 21, 2015 5:29 pm
by Joe Black
the randomness of math.random is correct if you print segNo just after the affectation line 21 you got something like this

Code: Select all

1
1
2
2
2
1
1
2
2
2
2
1
2
1
1
1
1
2
1
1
2
1
2
1
2
1
1
2
1
2
1
1
2
1
1
1
1
1
2
1
1
1
2
2
1
1
2
2
2
1
1
2
2
2
2
1
2
1
1
1
1
2
1
1
2
1
2
1
2
1
1
2
1
2
1
1
2
1
1
1
1
1
2
1
1
1
2
2
1
1
1
1
1
2
2
2
1
1
2
2
2
2
1
2
1
1
1
1
2
1
1
2
1
2
1
2
1
1
2
1
2
1
1
2
1
1
1
1
1
2
1
1
1
2
2
1
1
1
1
1
1
but I didn't go through the code further

Re: math.random fluctuation

Posted: Mon Jun 22, 2015 7:01 am
by MattyAB
Sorry... How does this help me? It is useful to know what the output is, but I don't understand why it is useful. I want to know how to fix the fluctuation, not what the output is.

Thanks!

Re: math.random fluctuation

Posted: Mon Jun 22, 2015 8:06 am
by bobbyjones
Math.random should fluctuate I believe.

Re: math.random fluctuation

Posted: Mon Jun 22, 2015 12:23 pm
by s-ol
I dont see what your problem is here. Game seems to be working fine and math.random(1,2) works exactly as expected, it returns an integer between 1 and 2 (aka one of both) at random. math.random obviously may return a different number every time, that is what it's for.

Re: math.random fluctuation

Posted: Mon Jun 22, 2015 5:36 pm
by micha
MattyAB wrote:I put in some debug code with prints, and saw that when it randomises the number, the variable fluctuates between 1 and 2 for a few seconds before settling on 1 number. By this time, all of the columns of blue have spawned in 1 place.
I had a look at it and I cannot reproduce the problem. Can you explain in more detail, how to reproduce it?

Re: math.random fluctuation

Posted: Mon Jun 22, 2015 6:23 pm
by MattyAB
This is really strange. It sometimes does not do any randomisation and just puts the second number back to 3, but sometimes does the weird fluctuating thing. Press space to play, then watch it through a few times and you will see it happens after the 2nd number is 12. Attached will be a .love just in case I've made any unknown changes that I forgot about.

Thanks!

Re: math.random fluctuation

Posted: Wed Jun 24, 2015 4:05 pm
by MattyAB
Does anyone know why this is happening? Help would be appreciated.

Re: math.random fluctuation

Posted: Thu Jun 25, 2015 9:44 am
by micha
I don't have a complete solution for you but I noticed something: Whenever the random flickering occurs, at the right side of the screen two block appear one on top of the other (usually leaving no gap at all). So I think your block-generation logic is broken somewhere. You should start digging there.