Question

i = (gameTime.TotalGameTime.Milliseconds / n) % 11

the modulo here returns the following numbers: {1,2,3,4,5,6,7,8,9,10,0,1,0} But those last zeros and the one are bothering me because it shouldn't be like this, should it? My wanted result is returning {1,2,3,4,5,6,7,8,9,10,11,0}, but it doesn't. Has anyone an explanation for it? I have totally no idea what causes it, I've already tried different versions of this code, also changing the 11 to something else but it didn't work out too.

Was it helpful?

Solution 2

Your modulo value should be 12, not 11. Modulo returns the remainder of the division (More precisely the equation of m % n is m - floor(m/n)n), so 11 / 11 will indeed return 0.

However, with 12, 11 / 12 will be 11, 12 / 12 is 0 and 13 / 12 have 1 as the remainder. It 'wraps around' your value - 1

So you should use this instead :

i = (gameTime.TotalGameTime.Milliseconds / n) % 12

Examples with 12:

var b = 10 % 12; //10
var c = 11 % 12; //11
var d = 12 % 12; //0
var e = 13 % 12; //1

OTHER TIPS

No, because 11 % 11 is zero. See documentation: http://msdn.microsoft.com/en-us/library/0w4e0fzs.aspx

No, the % operator returns the remainder when dividing the left operand by the right operand. In other words, x % x always returns 0 becuase x is perfectly divisible by x. Or to put it more generally, for integers, n, m and x, (n * x + m) % x == m

If you want values which range from 0 to 11, use modulo 12:

i = (gameTime.TotalGameTime.Milliseconds / n) % 12
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top