## 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]

• 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.

The test code:

we got the output:

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

The test code:

we got the output:

### 2.3 Use Random to get [0,n)

The test code:

we got the output:

### 2.4 Use ThreadLocalRandom to get [0,n]

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.

The test code:

we got the output:

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

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

The test code:

we got the output:

### 2.6 Use ThreadLocalRandom to get [m,n]

You can also ThreadLocalRandom to generate a range numbers.

The test code:

we got the output:

## 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:
{"ft_lang_label":"__label__en","ft_lang_prob":0.75224996,"math_prob":0.8445078,"size":1883,"snap":"2019-51-2020-05","text_gpt3_token_len":448,"char_repetition_ratio":0.18999468,"word_repetition_ratio":0.14423077,"special_character_ratio":0.23207648,"punctuation_ratio":0.15113351,"nsfw_num_words":0,"has_unicode_error":false,"math_prob_llama3":0.99163514,"pos_list":[0],"im_url_duplicate_count":[null],"WARC_HEADER":"{\"WARC-Type\":\"response\",\"WARC-Date\":\"2020-01-20T21:40:47Z\",\"WARC-Record-ID\":\"<urn:uuid:dd39d394-f4f0-4af7-8ce6-446512f8a655>\",\"Content-Length\":\"23719\",\"Content-Type\":\"application/http; msgtype=response\",\"WARC-Warcinfo-ID\":\"<urn:uuid:47873834-085b-40af-891a-4a7a906bd568>\",\"WARC-Concurrent-To\":\"<urn:uuid:1534f9e6-6497-4a88-93f6-6570a4e0a633>\",\"WARC-IP-Address\":\"104.18.49.33\",\"WARC-Target-URI\":\"https://www.bswen.com/2018/05/java-6-ways-to-generate-random-numbers-in-java.html\",\"WARC-Payload-Digest\":\"sha1:JJTFZLXU7XCUHGR5GPQXDKB3KPPPRHS6\",\"WARC-Block-Digest\":\"sha1:HEKYC4FUNDRS4XVOWXLTH7ZMAMPNOKSP\",\"WARC-Identified-Payload-Type\":\"text/html\",\"warc_filename\":\"/cc_download/warc_2020/CC-MAIN-2020-05/CC-MAIN-2020-05_segments_1579250599789.45_warc_CC-MAIN-20200120195035-20200120224035-00491.warc.gz\"}"}