Jump to content

  • Log In with Google      Sign In   
  • Create Account

#ActualHodgman

Posted 28 April 2013 - 11:44 PM

 I would say just because it was drilled into our minds so much in school that you learn to use it while using the modulus isn't really drilled into your mind to use in school.  Only reason I can think of I guess...?

In that case, think of it as being basically the same operation as division. With integers, division has two results. You get the quotient with "/" and the remainder with "%".

With floats, division only has one result, you get the "quotient + remainder/divisor" with  "/".

 

You can also use it to bring a random int into a range:

This is a very common usage, but if you require a completely random statistical distribution of random numbers, then this can screw you with a bias.

e.g. say that rand returns numbers from 0-15, but you want numbers 0-12. In this case, the numbers 13/14/15 are wrapped around to 0/1/2, which makes these first three numbers twice as likely to be picked than the rest (3-12) are!

Usually this isn't a problem because rand generates really big numbers, making the bias very small and not noticeable  so I would still usually use modulo for this.

It's usually only a problem in very precise scientific simulators, where you'd want to cast the random number to a float and divide by RAND_MAX, to get a normalized range of 0-1, and then scale this value to the desired range.


#2Hodgman

Posted 28 April 2013 - 11:41 PM

 I would say just because it was drilled into our minds so much in school that you learn to use it while using the modulus isn't really drilled into your mind to use in school.  Only reason I can think of I guess...?

In that case, think of it as being basically the same operation as division. With integers, division has two results. You get the quotient with "/" and the remainder with "%".

With floats, division only has one result, you get the "quotient + remainder/divisor" with  "/".

 

You can also use it to bring a random int into a range:

This is a very common usage, but if you require a completely random statistical distribution of random numbers, then this can screw you with a bias.

e.g. say that rand returns numbers from 0-15, but you want numbers 0-12. In this case, the numbers 13/14/15 are wrapped around to 0/1/2, which makes these first three numbers twice as likely to come up then 3-12!

Usually this isn't a noticeable problem because rand generates really big numbers, so I would stick with using modulo for this. It's usually only a problem in very precise scientific simulators, where you'd want to cast the random number to a float and divide by RAND_MAX, to get a normalized range of 0-1, and then scale this value to the desired range.


#1Hodgman

Posted 28 April 2013 - 11:40 PM

quote name='Chad Smith' timestamp='1367211399' post='5057646']
 I would say just because it was drilled into our minds so much in school that you learn to use it while using the modulus isn't really drilled into your mind to use in school.  Only reason I can think of I guess...?
[/quote]In that case, think of it as being basically the same operation as division. With integers, division has two results. You get the quotient with "/" and the remainder with "%".

With floats, division only has one result, you get the "quotient + remainder/divisor" with  "/".

 

 

You can also use it to bring a random int into a range:

This is a very common usage, but if you require a completely random statistical distribution of random numbers, then this can screw you with a bias.

e.g. say that rand returns numbers from 0-15, but you want numbers 0-12. In this case, the numbers 13/14/15 are wrapped around to 0/1/2, which makes these first three numbers twice as likely to come up then 3-12!

Usually this isn't a noticeable problem because rand generates really big numbers, so I would stick with using modulo for this. It's usually only a problem in very precise scientific simulators, where you'd want to cast the random number to a float and divide by RAND_MAX, to get a normalized range of 0-1, and then scale this value to the desired range.


PARTNERS