I'm working on assignment that requires me to generate and store 50 random PPS Numbers in an array but I'm not sure as to how to go about doing this. Keep in mind that I'm not asking anyone to do this assignment for me, this is just the part I need help with

Thanks in adavance

Edited by Gl753

3 Years
Discussion Span
Last Post by Phaelax

Personal Public Servive Number, It's a number that's 7 characters in lenght with a letter at the end e.g 000000E
I know how to generate a random number but I don't know how to generate one with a letter at the end


If there's a letter in there then you cannot create it as a numeric type, it has to be a String. Generate the 7 digit number, convert it to String, add the letter on the end.
If that's an Irish PPS then there's a very clear description of how to generate the letter in the WikiPedia article

Edited by JamesCherrill


I looked up PPS on wiki to see how it is calculated. Once the proper number is calculated, you can simply cast it to a char to match it up with an ASCII table.

        Random r = new Random();

        StringBuilder sb = new StringBuilder(8);
        int x = 0;
        for(int i=0;i<7;i++){
            int n = r.nextInt(10);
            x = n * (8-i);
        // Add 64 to the modulus result and cast
        // to a char to convert the number to the
        // corresponding letter in the ASCII table
        sb.append((char)(x % 23 + 64));


I believe this method would do the least amount of casting/conversion between numbers and strings. However, it does leave the potential for the following to be generated: 0000000@

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.