mirror of
https://github.com/MightyPirates/OpenComputers.git
synced 2025-09-19 12:17:17 -04:00
Fixed math.random, closes #400.
This commit is contained in:
parent
da5f2590aa
commit
0321a7816c
@ -247,17 +247,18 @@ object LuaStateFactory {
|
|||||||
// necessarily thread-safe.
|
// necessarily thread-safe.
|
||||||
val random = new Random
|
val random = new Random
|
||||||
state.pushScalaFunction(lua => {
|
state.pushScalaFunction(lua => {
|
||||||
|
val r = random.nextDouble()
|
||||||
lua.getTop match {
|
lua.getTop match {
|
||||||
case 0 => lua.pushNumber(random.nextDouble())
|
case 0 => lua.pushNumber(r)
|
||||||
case 1 =>
|
case 1 =>
|
||||||
val u = lua.checkNumber(1).toInt
|
val u = lua.checkNumber(1)
|
||||||
lua.checkArg(1, 1 <= u, "interval is empty")
|
lua.checkArg(1, 1 <= u, "interval is empty")
|
||||||
lua.pushInteger(1 + random.nextInt(u))
|
lua.pushNumber(math.floor(r * u) + 1)
|
||||||
case 2 =>
|
case 2 =>
|
||||||
val l = lua.checkNumber(1).toInt
|
val l = lua.checkNumber(1)
|
||||||
val u = lua.checkNumber(2).toInt
|
val u = lua.checkNumber(2)
|
||||||
lua.checkArg(1, l <= u, "interval is empty")
|
lua.checkArg(2, l <= u, "interval is empty")
|
||||||
lua.pushInteger(l + random.nextInt(u - (l - 1)))
|
lua.pushNumber(math.floor(r * (u - l + 1)) + l)
|
||||||
case _ => throw new IllegalArgumentException("wrong number of arguments")
|
case _ => throw new IllegalArgumentException("wrong number of arguments")
|
||||||
}
|
}
|
||||||
1
|
1
|
||||||
@ -265,7 +266,7 @@ object LuaStateFactory {
|
|||||||
state.setField(-2, "random")
|
state.setField(-2, "random")
|
||||||
|
|
||||||
state.pushScalaFunction(lua => {
|
state.pushScalaFunction(lua => {
|
||||||
random.setSeed(lua.checkInteger(1))
|
random.setSeed(lua.checkNumber(1).toLong)
|
||||||
0
|
0
|
||||||
})
|
})
|
||||||
state.setField(-2, "randomseed")
|
state.setField(-2, "randomseed")
|
||||||
|
Loading…
x
Reference in New Issue
Block a user