【问题标题】:Trying to return integer with math.random, lua试图用 math.random,lua 返回整数
【发布时间】:2014-06-04 01:01:08
【问题描述】:

我编写了这段代码:

myHero = GetMyHero()

a = math.random()

function OnDraw()
    if (myHero.health < 2000) then
        DrawText(a, 30, 600, 200, 0xFFFF0000)
    end
end

math.random() 给出浮点数,我需要一个整数。我怎样才能得到一个整数?


编辑:

事实证明,math.random(upper) 和 (x, y) 给出整数。

【问题讨论】:

  • 请至少做最少的研究。

标签: math random lua floating-point integer


【解决方案1】:

假设它返回一个浮点数 0 =

【讨论】:

    【解决方案2】:

    请查看 Lua 文档。它易于阅读。

    Lua 5.2 Reference Manual

    math.random ([m [, n]])

    此函数是标准 C 提供的简单伪随机生成器函数 rand 的接口。(不能保证其统计特性。)

    当不带参数调用时,返回 [0,1) 范围内的统一伪随机实数。当使用整数 m 调用时,math.random 返回范围 [1, m] 内的统一伪随机整数。当使用两个整数 m 和 n 调用时,math.random 返回 [m, n] 范围内的统一伪随机整数。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2018-01-22
      • 2015-05-18
      • 2016-03-04
      • 2014-03-15
      • 2022-09-28
      • 1970-01-01
      • 2023-03-06
      相关资源
      最近更新 更多