1. 程式人生 > >Java 隨機數生成器

Java 隨機數生成器

public class Random
{

private static final int A=48271;
private static final int M=2147483647;
private static final int Q=M/A;
private static final int R=M%A;
private int state;

public Random()
{
state=(int)( System.currentTimeMillis( ) % Integer.MAX_VALUE );
}
public int randomInt()
{

int tmpState=A*(state%Q)-R*(state/Q);
if(tmpState>=0)
state=tmpState;
else

state=tmpState+M;
return state;

}

public static void main( String [] args )
{
Random random=new Random();
for(int i=0;i<random.randomInt();i++)
{
System.out.println(random.randomInt());
}
}

}