math.random fluctuation

Questions about the LÖVE API, installing LÖVE and other support related questions go here.
Forum rules
Before you make a thread asking for help, read this.
Post Reply
User avatar
MattyAB
Citizen
Posts: 57
Joined: Wed Mar 25, 2015 8:37 pm

math.random fluctuation

Post 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!
Attachments
game.love
(63.02 KiB) Downloaded 129 times
Check out my blog - The place to find out about the latest tech news! http://thegeekcircle.blogspot.co.uk/
Joe Black
Prole
Posts: 39
Joined: Wed Jan 21, 2015 1:57 pm

Re: math.random fluctuation

Post 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
User avatar
MattyAB
Citizen
Posts: 57
Joined: Wed Mar 25, 2015 8:37 pm

Re: math.random fluctuation

Post 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!
Check out my blog - The place to find out about the latest tech news! http://thegeekcircle.blogspot.co.uk/
bobbyjones
Party member
Posts: 730
Joined: Sat Apr 26, 2014 7:46 pm

Re: math.random fluctuation

Post by bobbyjones »

Math.random should fluctuate I believe.
User avatar
s-ol
Party member
Posts: 1077
Joined: Mon Sep 15, 2014 7:41 pm
Location: Cologne, Germany
Contact:

Re: math.random fluctuation

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

s-ol.nu /blog  -  p.s-ol.be /st8.lua  -  g.s-ol.be /gtglg /curcur

Code: Select all

print( type(love) )
if false then
  baby:hurt(me)
end
User avatar
micha
Inner party member
Posts: 1083
Joined: Wed Sep 26, 2012 5:13 pm

Re: math.random fluctuation

Post 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?
User avatar
MattyAB
Citizen
Posts: 57
Joined: Wed Mar 25, 2015 8:37 pm

Re: math.random fluctuation

Post 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!
Attachments
game.love
(49.1 KiB) Downloaded 112 times
Check out my blog - The place to find out about the latest tech news! http://thegeekcircle.blogspot.co.uk/
User avatar
MattyAB
Citizen
Posts: 57
Joined: Wed Mar 25, 2015 8:37 pm

Re: math.random fluctuation

Post by MattyAB »

Does anyone know why this is happening? Help would be appreciated.
Check out my blog - The place to find out about the latest tech news! http://thegeekcircle.blogspot.co.uk/
User avatar
micha
Inner party member
Posts: 1083
Joined: Wed Sep 26, 2012 5:13 pm

Re: math.random fluctuation

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

Who is online

Users browsing this forum: No registered users and 51 guests