Download as pdf or txt
Download as pdf or txt
You are on page 1of 8

ASSIGNMENT-1

Name: Raj Kalash Tiwari


Enrollment No:190180107074
Subject: Analysis And Design Of Algorithms (3150703)
Department: Computer Engineering

Question:

Create a program (any language) which you can use to generate sequence of
random numbers of 1,2,3,4,5 and 6 digits. (example
9,99,999,9999,99999,999999) The program should generate random number of
given digit into a file. Create multiple files with 100,1000, 100000 and 10000000
entries of numbers. We will keep the files for future use. in sorting problems.
Submit your code and screenshot of file created.

Code:
package hello;
import java.io.*;

public class RandomNumber6Digit {

public static void main(String[] args) {


System.out.println("-------Create a file with 100 random numbers.-----");
try(
PrintWriter pw =new PrintWriter(new FileOutputStream(new
File("11.txt"), true));
)
{
for(int i=0;i<100;i++)
pw.print((int)(Math.random()*(1000000-1))+ " ");
}
catch(FileNotFoundException fnfe)
{
System.out.println("Cannot Create the file.");
fnfe.printStackTrace();
}
System.out.println("-------Create a file with 1000 random numbers.-----");
try(
PrintWriter pw =new PrintWriter(new FileOutputStream(new
File("12.txt"), true));
)
{
for(int i=0;i<1000;i++)
pw.print((int)(Math.random()*999999)+ " ");
}
catch(FileNotFoundException fnfe)
{
System.out.println("Cannot Create the file.");
fnfe.printStackTrace();
}
System.out.println("-------Create a file with 100000 random numbers.-----");
try(
PrintWriter pw =new PrintWriter(new FileOutputStream(new
File("13.txt"), true));
)
{
for(int i=0;i<100000;i++)
pw.print((int)(Math.random()*999999)+ " ");
}
catch(FileNotFoundException fnfe)
{
System.out.println("Cannot Create the file.");
fnfe.printStackTrace();
}
System.out.println("-------Create a file with 10000000 random numbers.-----");
try(
PrintWriter pw =new PrintWriter(new FileOutputStream(new
File("14.txt"), true));
)
{
for(int i=0;i<10000000;i++)
pw.print((int)(Math.random()*999999)+ " ");
}
catch(FileNotFoundException fnfe)
{
System.out.println("Cannot Create the file.");
fnfe.printStackTrace();
}
System.out.println("-------Create a file with 100 random numbers.-----");
try(
PrintWriter pw =new PrintWriter(new FileOutputStream(new
File("21.txt"), true));
)
{
for(int i=0;i<100;i++)
pw.print((int)(Math.random()*(1000000-1))+ " ");
}
catch(FileNotFoundException fnfe)
{
System.out.println("Cannot Create the file.");
fnfe.printStackTrace();
}
System.out.println("-------Create a file with 1000 random numbers.-----");
try(
PrintWriter pw =new PrintWriter(new FileOutputStream(new
File("22.txt"), true));
)
{
for(int i=0;i<1000;i++)
pw.print((int)(Math.random()*999999)+ " ");
}
catch(FileNotFoundException fnfe)
{
System.out.println("Cannot Create the file.");
fnfe.printStackTrace();
}
System.out.println("-------Create a file with 100000 random numbers.-----");
try(
PrintWriter pw =new PrintWriter(new FileOutputStream(new
File("23.txt"), true));
)
{
for(int i=0;i<100000;i++)
pw.print((int)(Math.random()*999999)+ " ");
}
catch(FileNotFoundException fnfe)
{
System.out.println("Cannot Create the file.");
fnfe.printStackTrace();
}
System.out.println("-------Create a file with 10000000 random numbers.-----");
try(
PrintWriter pw =new PrintWriter(new FileOutputStream(new
File("24.txt"), true));
)
{
for(int i=0;i<10000000;i++)
pw.print((int)(Math.random()*999999)+ " ");
}
catch(FileNotFoundException fnfe)
{
System.out.println("Cannot Create the file.");
fnfe.printStackTrace();
}
System.out.println("-------Create a file with 100 random numbers.-----");
try(
PrintWriter pw =new PrintWriter(new FileOutputStream(new
File("31.txt"), true));
)
{
for(int i=0;i<100;i++)
pw.print((int)(Math.random()*(99999))+ " ");
}
catch(FileNotFoundException fnfe)
{
System.out.println("Cannot Create the file.");
fnfe.printStackTrace();
}
System.out.println("-------Create a file with 1000 random numbers.-----");
try(
PrintWriter pw =new PrintWriter(new FileOutputStream(new
File("32.txt"), true));
)
{
for(int i=0;i<1000;i++)
pw.print((int)(Math.random()*99999)+ " ");
}
catch(FileNotFoundException fnfe)
{
System.out.println("Cannot Create the file.");
fnfe.printStackTrace();
}
System.out.println("-------Create a file with 100000 random numbers.-----");
try(
PrintWriter pw =new PrintWriter(new FileOutputStream(new
File("33.txt"), true));
)
{
for(int i=0;i<100000;i++)
pw.print((int)(Math.random()*99999)+ " ");
}
catch(FileNotFoundException fnfe)
{
System.out.println("Cannot Create the file.");
fnfe.printStackTrace();
}
System.out.println("-------Create a file with 10000000 random numbers.-----");
try(
PrintWriter pw =new PrintWriter(new FileOutputStream(new
File("34.txt"), true));
)
{
for(int i=0;i<10000000;i++)
pw.print((int)(Math.random()*99999)+ " ");
}
catch(FileNotFoundException fnfe)
{
System.out.println("Cannot Create the file.");
fnfe.printStackTrace();
}
System.out.println("-------Create a file with 100 random numbers.-----");
try(
PrintWriter pw =new PrintWriter(new FileOutputStream(new
File("41.txt"), true));
)
{
for(int i=0;i<100;i++)
pw.print((int)(Math.random()*(10000-1))+ " ");
}
catch(FileNotFoundException fnfe)
{
System.out.println("Cannot Create the file.");
fnfe.printStackTrace();
}
System.out.println("-------Create a file with 1000 random numbers.-----");
try(
PrintWriter pw =new PrintWriter(new FileOutputStream(new
File("42.txt"), true));
)
{
for(int i=0;i<1000;i++)
pw.print((int)(Math.random()*9999)+ " ");
}
catch(FileNotFoundException fnfe)
{
System.out.println("Cannot Create the file.");
fnfe.printStackTrace();
}
System.out.println("-------Create a file with 100000 random numbers.-----");
try(
PrintWriter pw =new PrintWriter(new FileOutputStream(new
File("43.txt"), true));
)
{
for(int i=0;i<100000;i++)
pw.print((int)(Math.random()*9999)+ " ");
}
catch(FileNotFoundException fnfe)
{
System.out.println("Cannot Create the file.");
fnfe.printStackTrace();
}
System.out.println("-------Create a file with 10000000 random numbers.-----");
try(
PrintWriter pw =new PrintWriter(new FileOutputStream(new
File("44.txt"), true));
)
{
for(int i=0;i<10000000;i++)
pw.print((int)(Math.random()*9999)+ " ");
}
catch(FileNotFoundException fnfe)
{
System.out.println("Cannot Create the file.");
fnfe.printStackTrace();
}
System.out.println("-------Create a file with 100 random numbers.-----");
try(
PrintWriter pw =new PrintWriter(new FileOutputStream(new
File("51.txt"), true));
)
{
for(int i=0;i<100;i++)
pw.print((int)(Math.random()*(1000-1))+ " ");
}
catch(FileNotFoundException fnfe)
{
System.out.println("Cannot Create the file.");
fnfe.printStackTrace();
}
System.out.println("-------Create a file with 1000 random numbers.-----");
try(
PrintWriter pw =new PrintWriter(new FileOutputStream(new
File("52.txt"), true));
)
{
for(int i=0;i<1000;i++)
pw.print((int)(Math.random()*999)+ " ");
}
catch(FileNotFoundException fnfe)
{
System.out.println("Cannot Create the file.");
fnfe.printStackTrace();
}
System.out.println("-------Create a file with 100000 random numbers.-----");
try(
PrintWriter pw =new PrintWriter(new FileOutputStream(new
File("53.txt"), true));
)
{
for(int i=0;i<100000;i++)
pw.print((int)(Math.random()*999)+ " ");
}
catch(FileNotFoundException fnfe)
{
System.out.println("Cannot Create the file.");
fnfe.printStackTrace();
}
System.out.println("-------Create a file with 10000000 random numbers.-----");
try(
PrintWriter pw =new PrintWriter(new FileOutputStream(new
File("54.txt"), true));
)
{
for(int i=0;i<10000000;i++)
pw.print((int)(Math.random()*999)+ " ");
}
catch(FileNotFoundException fnfe)
{
System.out.println("Cannot Create the file.");
fnfe.printStackTrace();
}
System.out.println("-------Create a file with 100 random numbers.-----");
try(
PrintWriter pw =new PrintWriter(new FileOutputStream(new
File("61.txt"), true));
)
{
for(int i=0;i<100;i++)
pw.print((int)(Math.random()*(100-1))+ " ");
}
catch(FileNotFoundException fnfe)
{
System.out.println("Cannot Create the file.");
fnfe.printStackTrace();
}
System.out.println("-------Create a file with 1000 random numbers.-----");
try(
PrintWriter pw =new PrintWriter(new FileOutputStream(new
File("62.txt"), true));
)
{
for(int i=0;i<1000;i++)
pw.print((int)(Math.random()*99)+ " ");
}
catch(FileNotFoundException fnfe)
{
System.out.println("Cannot Create the file.");
fnfe.printStackTrace();
}
System.out.println("-------Create a file with 100000 random numbers.-----");
try(
PrintWriter pw =new PrintWriter(new FileOutputStream(new
File("63.txt"), true));
)
{
for(int i=0;i<100000;i++)
pw.print((int)(Math.random()*99)+ " ");
}
catch(FileNotFoundException fnfe)
{
System.out.println("Cannot Create the file.");
fnfe.printStackTrace();
}
System.out.println("-------Create a file with 10000000 random numbers.-----");
try(
PrintWriter pw =new PrintWriter(new FileOutputStream(new
File("64.txt"), true));
)
{
for(int i=0;i<10000000;i++)
pw.print((int)(Math.random()*99)+ " ");
}
catch(FileNotFoundException fnfe)
{
System.out.println("Cannot Create the file.");
fnfe.printStackTrace();
}
System.out.println("-------Create a file with 100 random numbers.-----");
try(
PrintWriter pw =new PrintWriter(new FileOutputStream(new
File("71.txt"), true));
)
{
for(int i=0;i<100;i++)
pw.print((int)(Math.random()*(10-1))+ " ");
}
catch(FileNotFoundException fnfe)
{
System.out.println("Cannot Create the file.");
fnfe.printStackTrace();
}
System.out.println("-------Create a file with 1000 random numbers.-----");
try(
PrintWriter pw =new PrintWriter(new FileOutputStream(new
File("72.txt"), true));
)
{
for(int i=0;i<1000;i++)
pw.print((int)(Math.random()*9)+ " ");
}
catch(FileNotFoundException fnfe)
{
System.out.println("Cannot Create the file.");
fnfe.printStackTrace();
}
System.out.println("-------Create a file with 100000 random numbers.-----");
try(
PrintWriter pw =new PrintWriter(new FileOutputStream(new
File("73.txt"), true));
)
{
for(int i=0;i<100000;i++)
pw.print((int)(Math.random()*9)+ " ");
}
catch(FileNotFoundException fnfe)
{
System.out.println("Cannot Create the file.");
fnfe.printStackTrace();
}
System.out.println("-------Create a file with 10000000 random numbers.-----");
try(
PrintWriter pw =new PrintWriter(new FileOutputStream(new
File("74.txt"), true));
)
{
for(int i=0;i<10000000;i++)
pw.print((int)(Math.random()*9)+ " ");
}
catch(FileNotFoundException fnfe)
{
System.out.println("Cannot Create the file.");
fnfe.printStackTrace();
}

}
OUTPUT: -

You might also like