-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathchapter3.lua
More file actions
49 lines (42 loc) · 1.33 KB
/
chapter3.lua
File metadata and controls
49 lines (42 loc) · 1.33 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
function Practice_3_3()
for i = -10, 10 do
print(i, i % 3)
end
end
local math = require("math")
function Practice_3_6()
-- 通过高 、 母线与轴线的夹角来计算正圆锥体体积的函数 。
local slantHeight = 10 -- 母线长度,单位:米
local height = 5 -- 高度,单位:米
local angleInDegrees = 60 -- 母线与轴线的夹角,单位:度
local v = calculateConeVolume(slantHeight, height, angleInDegrees)
print(v)
end
function claculateRadius(l, thetaInDegrees)
local theraInRadians = thetaInDegrees * math.pi / 180
local r = l / math.tan(theraInRadians)
return r
end
function calculateConeVolume(l, h, thetaInDegrees)
local r = claculateRadius(l, thetaInDegrees)
local volume = (1 / 3) * math.pi * r * r * h
return volume
end
function Practice_3_7()
--利用 函数 math.random 编写一个生成遵循正态分布(高斯分布)的伪随机数发生器
-- Box-Muller变换
math.randomseed(os.time())
print (genGaussianRandom())
end
function genRandomNum()
local r1 = math.random()
local r2 = math.random()
return r1, r2
end
function genGaussianRandom()
local r1, r2 = genRandomNum()
local z0 = math.sqrt(-2 * math.log(r1) * math.cos(2 * math.pi * r2))
return z0
end
-- Practice_3_6()
Practice_3_7()