Lua math library (standard library-related)

Math Library

In this chapter (below about standard library chapters of the same) my main purpose is not to give a complete description of each function, but to tell you what the standard library provides functions. In order to be able to clearly explain the problem, I might ignore some minor options or behavior. The main idea is to stimulate your curiosity, these curious place might find the answer in the reference manual.

Set by the standard math library composed of arithmetic functions, such as trigonometric library (sin, cos, tan, asin, acos, etc.), power exponential function (exp, log, log10), rounding function (floor, ceil), max , min, plus a variable pi. A math library also defines power operator (^).

All trigonometric functions are working in radians. (Lua4.0 used to work in degrees.) You can use deg and rad functions between degrees and radians. If you want to use the trigonometric functions in degree case, you can re-define the trigonometric functions:

local sin, asin, ... = math.sin, math.asin, ... 
local deg, rad = math.deg, math.rad 
math.sin = function (x) return sin(rad(x)) end
math.asin = function (x) return deg(asin(x)) end
... 
math.random 用来产生伪随机数,有三种调用方式:
第一:不带参数,将产生 [0,1)范围内的随机数. 
第二:带一个参数 n,将产生 1 <= x <= n 范围内的随机数 x. 
第三:带两个参数 a 和 b,将产生 a <= x <= b 范围内的随机数 x. 

You can use randomseed set the random number generator seed, can only accept a numeric argument. Typically when the program begins, with a fixed seed to initialize the random number generator, means that each run the program will produce the same sequence of random numbers. To facilitate debugging, this is very good, but in the game it means that each run has the same level. A common technique to solve this problem is to use the current system time as the seed: (. Os.time function returns a digital representation of the current system time, usually an integer since the new era) math.randomseed (os.time ())

Published 290 original articles · won praise 153 · views 10000 +

Guess you like

Origin blog.csdn.net/qq_39885372/article/details/104416520