Paid for writing

In case you like my writing and would like me to write for your website, then please leave a comment to any of my blog article, mentioning your Email Id and I will reply back. Thanks

Thursday, January 29, 2009

Generating Random Nos. in Java

//RandomGen.java
//Algorithm in Robert Segewick


import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.math.BigInteger;
public class RandomGen
{
BigInteger m,m1,b,a;
int i,N;

public RandomGen()
{
m=new BigInteger("100000000");
m1=new BigInteger("10000");
b=new BigInteger("31415821");
}


public static void main(String args[])
{
RandomGen rg=new RandomGen();
try
{
rg.read();
}
catch(Exception e)
{

}
}

public void read() throws Exception
{
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));

System.out.print("Enter the total no: ");
N=Integer.parseInt(br.readLine());

System.out.print("Enter the initial no: ");
a=new BigInteger(br.readLine());

for(i=1;i<=N;i++)
{
System.out.println(random());
}
}

public String random()
{
BigInteger temp=mult();
a=temp.mod(m);

return a.toString();
}

public BigInteger mult()
{
BigInteger p,p0,p1,q,q0,q1;

p=a;
q=b;

p1=p.divide(m1);
p0=p.mod(m1);
q1=q.divide(m1);
q0=q.mod(m1);

BigInteger temp=(p0.multiply(q1).add(p1.multiply(q0)).multiply(m1).add(p0.multiply(q0))).mod(m);

return temp;
}
}

0 comments: