Saturday, 8 February 2014

Random Number Generation


import java.util.Random;

public class Randomnumber {

    public static void main(String[] args) {
       long r = generateRandom(12);
        System.out.println("r = " + r);
    }

    public static long generateRandom(int length) {
        Random random = new Random();
        char[] digits = new char[length];
        digits[0] = (char) (random.nextInt(9) + '1');
        for (int i = 1; i < length; i++) {
            digits[i] = (char) (random.nextInt(10) + '0');
        }
        return Long.parseLong(new String(digits));
    }
}

No comments:

Post a Comment