Generating random numbers between 999999 - 1000000 with an alpha number at the end
Hey every I'm stuck with trying to generate 1000 random numbers that have 7 digits my code is here below
I don't know how to set it to a fix digit number, I know it's simple but it's doing my head in
can anyone help me? thanks
I want numbers between 0000001 - 9999999 if possible with a character at the end
Ok the output is in a txt file
Random PPS Number : 1911462N
Random PPS Number : 2714102K
Random PPS Number : 5070317E
Random PPS Number : 8030837K
Random PPS Number : 4358369S
Random PPS Number : 5350002N
but my problem is I don't know how to sort it in lexicographical order
Grot
Code java:
import java.io.FileNotFoundException;
import java.io.PrintWriter;
import java.util.Random;
public class Assignment_Part2{
/**
* @param args
*/
private static PrintWriter outputStream;
public static void main(String[] args) {
String fileName = "pps.txt";//input the data to a file named text.txt
try {//using the try catch operator
outputStream = new PrintWriter(fileName);// using the printwriter method to insert data to the file
Random r = new Random();
String alphabet = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
for (int i = 0; i < 1; i++) {
} // prints random characters from alphabet
for (int j = 1; j <= 1000; j++){// using a for loop to gernerate numbers from 9999999 - 1000000
int myInt = 1000000 + (int)(Math.random() * ((9999999 - 1000000) + 1));
outputStream.println("Random PPS Number : " + myInt + alphabet.charAt(r.nextInt(alphabet.length())) + " ");//output stream to print the numbers to the pps.txt file
}
// stores in Ram First
outputStream.close(); // flushes the data to the file
System.out.println("PPS Numbers Are Printed Out In A Text File");//outputs to the user that it's done
} catch (FileNotFoundException e) {// the catch operator is used to give the user an error
System.out.println("File Not Found");//outputs this if an error is found
}
}
}
Re: Generating random numbers between 999999 - 1000000 with an alpha number at the end
I edited it there for you thanks
Grot
--- Update ---
Code java:
import java.io.FileNotFoundException;
import java.io.PrintWriter;
import java.util.Random;
public class Assignment_Part2{
/**
* @param args
*/
private static PrintWriter outputStream;
public static void main(String[] args) {
String fileName = "pps.txt";//input the data to a file named text.txt
try {//using the try catch operator
outputStream = new PrintWriter(fileName);// using the printwriter method to insert data to the file
Random r = new Random();
String alphabet = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
for (int i = 0; i < 1; i++) {
} // prints random characters from alphabet
for (int j = 1; j <= 1000; j++){// using a for loop to gernerate numbers from 9999999 - 1000000
int myInt = 1000000 + (int)(Math.random() * ((9999999 - 1000000) + 1));
outputStream.println("Random PPS Number : " + myInt + alphabet.charAt(r.nextInt(alphabet.length())) + " ");//output stream to print the numbers to the pps.txt file
}
// stores in Ram First
outputStream.close(); // flushes the data to the file
System.out.println("PPS Numbers Are Printed Out In A Text File");//outputs to the user that it's done
} catch (FileNotFoundException e) {// the catch operator is used to give the user an error
System.out.println("File Not Found");//outputs this if an error is found
}
}
}
Fixed numbers I mean from 1000000 - 9999999
I fixed it there.
my problem is how do I sort it in lexicographical order
Thanks
Grot
Re: Generating random numbers between 999999 - 1000000 with an alpha number at the end
Quote:
how to sort it in lexicographical order
Where are the numbers stored so you can sort them?
What does the program output when it executes?
Re: Generating random numbers between 999999 - 1000000 with an alpha number at the end
The numbers are stored in a txt file e.g
Random PPS Number : 1911462N
Random PPS Number : 2714102K
Random PPS Number : 5070317E
Random PPS Number : 8030837K
Random PPS Number : 4358369S
Re: Generating random numbers between 999999 - 1000000 with an alpha number at the end
To sort them, they need to be in some kind of data structure in the program like an array or a collection.
Re: Generating random numbers between 999999 - 1000000 with an alpha number at the end