6 ways to generate random numbers in java

1. Introduction

This post would demo how to generate random integer numbers using java.

The ways include:

  • Use the System.currentTimeInMillis to get [0,n)
  • Use Math.random to get [0,n)
  • Use Random to get [0,n)
  • Use ThreadLocalRandom to get [0,n]
  • Use Math.random to get [m,n]
  • Use ThreadLocalRandom to get [m,n]

1. Environments

  • Java 1.8

2. Example codes

2.1 Use the System.currentTimeInMillis to get [0,n)

This way would not generate a real random number, just the milli seconds of current time.

    private static int generateByTime(int upperLimit) {
        final long l = System.currentTimeMillis();
        return (int)( l % upperLimit );
    }

The test code:

int upperLimit = 100;
for(int i=0;i<10;i++) {
    System.out.println(generateByTime(upperLimit));
}

we got the output:

67
68
68
68
68
68
68
68
68
68

2.2 Use Math.random to get [0,n)

    private static int generateByMathRandom(int upperLimit) {
        final double d = Math.random();
        return (int)(d*upperLimit);
    }

The test code:

int upperLimit = 100;
for(int i=0;i<10;i++) {
    System.out.println(generateByMathRandom(upperLimit));
}

we got the output:

44
0
92
91
49
89
24
98
40
61

2.3 Use Random to get [0,n)

    //the java.util.Random class is often preferable to java.lang.Math.random().
    private static int generateByRandom(int upperLimit) {
        Random random = new Random();
        return random.nextInt(upperLimit);
    }

The test code:

int upperLimit = 100;
for(int i=0;i<10;i++) {
    System.out.println(generateByRandom(upperLimit));
}

we got the output:

6
19
71
59
16
66
11
81
31
60

2.4 Use ThreadLocalRandom to get [0,n]

The ThreadLocalRandom is provided since java 1.7, the javadoc

A random number generator isolated to the current thread. Like the global Random generator used by the Math class, a ThreadLocalRandom is initialized with an internally generated seed that may not otherwise be modified. When applicable, use of ThreadLocalRandom rather than shared Random objects in concurrent programs will typically encounter much less overhead and contention. Use of ThreadLocalRandom is particularly appropriate when multiple tasks (for example, each a ForkJoinTask) use random numbers in parallel in thread pools.

    private static int generateByThreadLocalRandom(int upperLimit) {
        //java 1.7 +
        return ThreadLocalRandom.current().nextInt(0, upperLimit + 1);
    }

The test code:

int upperLimit = 100;
for(int i=0;i<10;i++) {
    System.out.println(generateByThreadLocalRandom(upperLimit));
}

we got the output:

72
33
83
90
24
73
74
29
25
40

2.5 Use Math.random to get [m,n]

If you want to get a range of numbers,you can do like this:

    private static int generateByMathRandomRange(int rangeFrom,int rangeTo) {
        if(rangeFrom<0) throw new IllegalArgumentException("rangeFrom must greater than 0");
        return rangeFrom + (int)(Math.random() * ((rangeTo - rangeFrom) + 1));
    }

The test code:

int upperLimit = 100;
for(int i=0;i<10;i++) {
    System.out.println(generateByMathRandomRange(50,120));
}

we got the output:

89
90
81
56
90
71
59
51
90
102

2.6 Use ThreadLocalRandom to get [m,n]

You can also ThreadLocalRandom to generate a range numbers.

    private static int generateByThreadLocalRandom2(int rangeFrom,int rangeTo) {
        if(rangeFrom>=rangeTo) throw new IllegalArgumentException("bad param");
        //java 1.7 +
        return ThreadLocalRandom.current().nextInt(rangeFrom, rangeTo + 1);
    }

The test code:

int upperLimit = 100;
for(int i=0;i<10;i++) {
    System.out.println(generateByThreadLocalRandom2(-100,-10));
}

we got the output:

-18
-22
-60
-22
-75
-15
-77
-40
-46
-55

3. summary

In this post we demoed how to generate random numbers by using various methods.You can find the whole code examples on github project, the class source code is located at this

You can find detail documents about the java stream here: