Recognizing when to use the modulus operator

前端 未结 19 907
醉梦人生
醉梦人生 2020-11-29 16:40

I know the modulus (%) operator calculates the remainder of a division. How can I identify a situation where I would need to use the modulus operator?

I know I can u

19条回答
  •  抹茶落季
    2020-11-29 16:55

    There are many instances where it is useful.

    If you need to restrict a number to be within a certain range you can use mod. For example, to generate a random number between 0 and 99 you might say:

    num = MyRandFunction() % 100;
    

提交回复
热议问题