Random number using the Random function

xiaoxiao2021-03-06  78

Author: Dennis Bale translation: Candy Difficulty: Intermediate

The Random function is very useful to generate basic random numbers, create random movements, as well as random colors and other more functions. The basic random function is matH.random (); can generate any scales between 0-1, such as 0.0105901374530933 or 0.872525005541986, several other functions can be used to change the resulting numbers, which can be better in yours. Used in the film: math.Round (); Math.ceil (); Math.Floor (); these functions are used to achieve an integer, Math.Round (); is the closest integer in a four-way approach. Math.ceil (); is a closest integer, math.floor (); and math.ceil (); opposite, math.floor (); get a closest integer to combine these functions, you It can be written like this: Math.Round (Math.random ()); this expression can generate a number of 0.0 and 1.0, and then the four rounds will get an integer. The number thus generated is 0 or 1. This expression can be used, such as a hard coin, or TRUE / FALSE instructions in various possible conditions. * 10 is to multiply the decimal number of you generated by 10, and then goes to get an integer: Math.Round (Math.random () * 10); To create a random number between 1 and 10, you can write: Math. CEIL (Math.random () * 10); should be a value of Math.ceil, so it does not generate 0. To create a random number of 5 to 20, you can write Math.Round (Math.random () * 15) 5; that is, if you want to create a random number from X to Y, you can write Math.Round. (Math.random () * (YX)) X; X and Y can be any value, even if the negative number is also.

转载请注明原文地址:https://www.9cbs.com/read-93442.html

New Post(0)