generate Random Int - Java java.util

Java examples for java.util:Random Int

Description

generate Random Int

Demo Code


//package com.java2s;
import java.util.Date;
import java.util.Random;

public class Main {
    public static void main(String[] argv) {
        int length = 42;
        System.out.println(generateRandomInt(length));
    }// ww w .jav  a  2 s . co  m

    private static Random random = new Random((new Date()).getTime());

    public static int generateRandomInt(int length) {
        char[] values = { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9' };
        String out = "";
        for (int i = 0; i < length; i++) {
            int idx = random.nextInt(values.length);
            out += values[idx];
        }

        return Integer.parseInt(out);
    }
}

Related Tutorials