Skip to content

Commit

Permalink
Fix random on an empty table
Browse files Browse the repository at this point in the history
  • Loading branch information
dwursteisen committed Nov 30, 2023
1 parent 2d46963 commit bcc4680
Showing 1 changed file with 6 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -127,8 +127,12 @@ class MathLib : org.luaj.vm2.lib.MathLib() {
if (arg.isnil()) return call()
return if (arg.istable()) {
val table = arg.checktable()!!
val index = Random.nextInt(1, table.length() + 1)
table[index]
if (table.length() > 0) {
val index = Random.nextInt(1, table.length() + 1)
table[index]
} else {
NIL
}
} else {
if (arg.isint()) {
valueOf(Random.nextInt(abs(arg.toint())))
Expand Down

0 comments on commit bcc4680

Please sign in to comment.