Add math.random() and math.randomseed()
This commit is contained in:
@@ -21,6 +21,8 @@
|
|||||||
******************************************************************************/
|
******************************************************************************/
|
||||||
package org.luaj.lib;
|
package org.luaj.lib;
|
||||||
|
|
||||||
|
import java.util.Random;
|
||||||
|
|
||||||
import org.luaj.vm.LDouble;
|
import org.luaj.vm.LDouble;
|
||||||
import org.luaj.vm.LFunction;
|
import org.luaj.vm.LFunction;
|
||||||
import org.luaj.vm.LInteger;
|
import org.luaj.vm.LInteger;
|
||||||
@@ -43,6 +45,8 @@ public class MathLib extends LFunction {
|
|||||||
"sqrt",
|
"sqrt",
|
||||||
"ceil",
|
"ceil",
|
||||||
"floor",
|
"floor",
|
||||||
|
"random",
|
||||||
|
"randomseed",
|
||||||
};
|
};
|
||||||
|
|
||||||
private static final int INSTALL = 0;
|
private static final int INSTALL = 0;
|
||||||
@@ -55,6 +59,8 @@ public class MathLib extends LFunction {
|
|||||||
private static final int SQRT = 7;
|
private static final int SQRT = 7;
|
||||||
private static final int CEIL = 8;
|
private static final int CEIL = 8;
|
||||||
private static final int FLOOR = 9;
|
private static final int FLOOR = 9;
|
||||||
|
private static final int RANDOM = 10;
|
||||||
|
private static final int RANDOMSEED = 11;
|
||||||
|
|
||||||
public static void install( LTable globals ) {
|
public static void install( LTable globals ) {
|
||||||
LTable math = new LTable();
|
LTable math = new LTable();
|
||||||
@@ -65,6 +71,8 @@ public class MathLib extends LFunction {
|
|||||||
globals.put( "math", math );
|
globals.put( "math", math );
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private static Random random = null;
|
||||||
|
|
||||||
private final int id;
|
private final int id;
|
||||||
|
|
||||||
private MathLib( int id ) {
|
private MathLib( int id ) {
|
||||||
@@ -112,6 +120,34 @@ public class MathLib extends LFunction {
|
|||||||
case FLOOR:
|
case FLOOR:
|
||||||
setResult( vm, LInteger.valueOf( (int) Math.floor( vm.tonumber(2) ) ) );
|
setResult( vm, LInteger.valueOf( (int) Math.floor( vm.tonumber(2) ) ) );
|
||||||
break;
|
break;
|
||||||
|
case RANDOM: {
|
||||||
|
if ( random == null )
|
||||||
|
random = new Random();
|
||||||
|
switch ( vm.gettop() ) {
|
||||||
|
case 1:
|
||||||
|
vm.resettop();
|
||||||
|
vm.pushnumber(random.nextDouble());
|
||||||
|
break;
|
||||||
|
case 2: {
|
||||||
|
int m = vm.tointeger(2);
|
||||||
|
vm.resettop();
|
||||||
|
vm.pushinteger(1+random.nextInt(m));
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
default: {
|
||||||
|
int m = vm.tointeger(2);
|
||||||
|
int n = vm.tointeger(3);
|
||||||
|
vm.resettop();
|
||||||
|
vm.pushinteger(m+random.nextInt(n+1-m));
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
case RANDOMSEED:
|
||||||
|
random = new Random( vm.tointeger(2) );
|
||||||
|
vm.resettop();
|
||||||
|
break;
|
||||||
default:
|
default:
|
||||||
LuaState.vmerror( "bad math id" );
|
LuaState.vmerror( "bad math id" );
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -2,3 +2,25 @@ print( math.sin( 0.0 ) )
|
|||||||
print( math.cos( math.pi ) )
|
print( math.cos( math.pi ) )
|
||||||
print( math.sqrt( 9.0 ) )
|
print( math.sqrt( 9.0 ) )
|
||||||
print( math.modf( 5.25 ) )
|
print( math.modf( 5.25 ) )
|
||||||
|
print( math.random(5,10) )
|
||||||
|
print( math.random(5,10) )
|
||||||
|
print( math.random(5,10) )
|
||||||
|
print( math.random(5,10) )
|
||||||
|
print( math.random() )
|
||||||
|
print( math.random() )
|
||||||
|
print( math.random() )
|
||||||
|
print( math.random() )
|
||||||
|
print( math.randomseed(20), math.random() )
|
||||||
|
print( math.randomseed(20), math.random() )
|
||||||
|
print( math.randomseed(20), math.random() )
|
||||||
|
print( math.randomseed(30), math.random() )
|
||||||
|
print( math.randomseed(30), math.random() )
|
||||||
|
print( math.random(30) )
|
||||||
|
print( math.random(30) )
|
||||||
|
print( math.random(30) )
|
||||||
|
print( math.random(30) )
|
||||||
|
print( math.random(-4,-2) )
|
||||||
|
print( math.random(-4,-2) )
|
||||||
|
print( math.random(-4,-2) )
|
||||||
|
print( math.random(-4,-2) )
|
||||||
|
print( math.random(-4,-2) )
|
||||||
|
|||||||
Reference in New Issue
Block a user