Subscribe to A Kid's scribblings

## Another algorithm to generate random numbers

A more popular method of generating a random number.Mathematically its somethin like this........

resultant=(a * seed) + (b % c);

According to the original method a,b & c are constants,but here i have taken them as funtions of seed.

f(seed)=a;f(seed)=b;f(seed)=c;

The code..............

```package Random;
import java.io.*;
public class random {
public static double rand()
{
double seed=0.0,res=0.0,a=0.0,b=0.0,c=0.0;
try{
File f=new File("C:\\Users\\Souvik\\Documents\\NetBeansProjects\\Gojkid\\src\\Random\\db.dat");
/*
* Here mention the path of the database of the seed.
*/
FileInputStream fis=new FileInputStream(f);
DataInputStream dis=new DataInputStream(fis);
a=seed*3;b=seed*13;c=seed*5;
res=(seed*a)+(b%c);
while(res<0.1)
res=res*10;
if(res>1.0)
res=((double)((int)res))-res;
if(res<0)
res=-res;
seed=res;
dis.close();
fis.close();
FileOutputStream fos=new FileOutputStream(f);
DataOutputStream dos=new DataOutputStream(fos);
dos.writeDouble(seed);
fos.close();
dos.close();
}
catch(Exception e)
{
System.err.println(e.getMessage());
}
return res;
}

}

```

### 1 Comments On This Entry

Page 1 of 1

#### gojabe

03 January 2012 - 09:46 PM
I have recently thought upon the idea about using the system time in milliseconds as the seed to generate a random number. I don't know this is a good technique or not,but this gonna save the system of maintaining a binary file to generate a random number the next time. Thus the code should be freed of the i/o streams, thus increasing the overall performance.
0
Page 1 of 1

S M T W T F S
12345
6789101112
13141516171819
20212223242526
272829 30 31

### 0 user(s) viewing

0 Guests
0 member(s)
0 anonymous member(s)