Switch to full style
Java2 codes,problems ,discussions and solutions are here
Post a reply

Random to File

Sat Jun 09, 2007 2:00 pm

This is a class the i made which write random numbers to file.Each number is written to file you can determine its base and its limit ,

Here is the class
java code
/*
* generate.java
*
* Created on February 16, 2007, 11:20 AM
*
* To change this template, choose Tools | Template Manager
* and open the template in the editor.
*/

package rand;

import java.io.BufferedWriter;
import java.io.FileWriter;
import java.io.IOException;
import java.util.Random;

/**
*
*/
public class generate {



private Random random=new Random();


public String Generate(int base,int limit) throws IOException
{



int Z=0;


while(true)
{
Z=random.nextInt(limit);
if(Z>=base&&Z<=limit)
{
System.out.println(Z);
return Integer.toString(Z);
}
}



}
public static void main(String[] args)
{
try {
String filename="rand.txt";
BufferedWriter myBuff=new BufferedWriter(new FileWriter(filename,false));

generate mygen=new generate();
myBuff.write(mygen.Generate(7,10));//1
myBuff.newLine();
myBuff.write(mygen.Generate(12,50));//2
myBuff.newLine();
myBuff.write(mygen.Generate(100,102));//3
myBuff.newLine();
myBuff.write(mygen.Generate(1,5));//4
myBuff.newLine();
myBuff.write(mygen.Generate(7,10));//5
myBuff.newLine();
myBuff.write(mygen.Generate(7,10));//6
myBuff.newLine();
myBuff.write(mygen.Generate(7,10));//7
myBuff.newLine();
myBuff.write(mygen.Generate(7,10));//8
myBuff.newLine();
myBuff.write(mygen.Generate(7,10));//9
myBuff.newLine();
myBuff.write(mygen.Generate(7,10));//10
myBuff.newLine();
myBuff.write(mygen.Generate(7,10));//11
myBuff.newLine();
myBuff.write(mygen.Generate(7,10));//12
myBuff.newLine();
myBuff.write(mygen.Generate(7,10));//13
myBuff.newLine();
myBuff.write(mygen.Generate(7,10));//14
myBuff.newLine();


myBuff.flush();
myBuff.close();
} catch (IOException ex) {
ex.printStackTrace();
}
}


}




Post a reply
  Related Posts  to : Random to File
 Random To File     -  
 Encrypt/Decrypt a file from source file to target file.     -  
 php Random quote     -  
 php Random image     -  
 Generating Random Number in java     -  
 random forest algorithm classifier     -  
 Pick one or more random entries out of an array     -  
 Applet Game To Shot a Random Ball     -  
 Random Search for tuning classifier parameters     -  
 Get the important variables of random forest classifier     -  

Topic Tags

Java Files and I/O