Add math.random() and math.randomseed()

This commit is contained in:
James Roseborough
2007-12-08 01:42:51 +00:00
parent 215134feb5
commit 1923d8e6a0
2 changed files with 68 additions and 10 deletions

View File

@@ -21,6 +21,8 @@
******************************************************************************/
package org.luaj.lib;
import java.util.Random;
import org.luaj.vm.LDouble;
import org.luaj.vm.LFunction;
import org.luaj.vm.LInteger;
@@ -43,18 +45,22 @@ public class MathLib extends LFunction {
"sqrt",
"ceil",
"floor",
"random",
"randomseed",
};
private static final int INSTALL = 0;
private static final int ABS = 1;
private static final int COS = 2;
private static final int MAX = 3;
private static final int MIN = 4;
private static final int MODF = 5;
private static final int SIN = 6;
private static final int SQRT = 7;
private static final int CEIL = 8;
private static final int FLOOR = 9;
private static final int INSTALL = 0;
private static final int ABS = 1;
private static final int COS = 2;
private static final int MAX = 3;
private static final int MIN = 4;
private static final int MODF = 5;
private static final int SIN = 6;
private static final int SQRT = 7;
private static final int CEIL = 8;
private static final int FLOOR = 9;
private static final int RANDOM = 10;
private static final int RANDOMSEED = 11;
public static void install( LTable globals ) {
LTable math = new LTable();
@@ -65,6 +71,8 @@ public class MathLib extends LFunction {
globals.put( "math", math );
}
private static Random random = null;
private final int id;
private MathLib( int id ) {
@@ -112,6 +120,34 @@ public class MathLib extends LFunction {
case FLOOR:
setResult( vm, LInteger.valueOf( (int) Math.floor( vm.tonumber(2) ) ) );
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:
LuaState.vmerror( "bad math id" );
}

View File

@@ -2,3 +2,25 @@ print( math.sin( 0.0 ) )
print( math.cos( math.pi ) )
print( math.sqrt( 9.0 ) )
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) )