Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to specify a range when java generates random integers

2025-04-07 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)05/31 Report--

In this article Xiaobian for you to introduce in detail "java generates random integers how to specify the range", the content is detailed, the steps are clear, the details are handled properly, I hope this "java generates random integers how to specify the range" article can help you solve your doubts, the following follow the editor's ideas slowly in-depth, together to learn new knowledge.

The operating environment of this tutorial: windows7 system, java10 version, DELL G3 computer.

1. Math.random () produces a random return value of 0-1, which is greater than or equal to 0.0 and less than 1.0.

For example: 0.5105802498623931.

Taking advantage of this feature, the random quantity in the following specific range is derived:

(1) generate a random integer from 0 to n, that is, return the value [0Magne]

Int num= (int) (Math.random () * (nasty 1)

(2) generate a random integer from a to b, that is, return the value [aforme b]

Int num=a+ (int) (Math.random () * (b-a+1))

2. Use the nextInt (intorigin,intbound) method of java.util.concurent.ThreadLocalRandom class.

The returned random quantity ranges from origin (inclusive) to bound (excluding)

For example, a number of 10 (inclusive)-99 (inclusive) is generated as follows:

Int randomNum = ThreadLocalRandom.current (). NextInt (10, 99 + 1); what are the collection classes in Java? the collections in Java are mainly divided into four categories:

1. List list: ordered and repeatable

2. Queue queue: orderly and repeatable

3. Set collection: non-repeatable

4. Map mapping: unordered, key unique, value not unique.

After reading this, the article "how to specify the range of random integers generated by java" has been introduced. If you want to master the knowledge of this article, you still need to practice and use it yourself to understand it. If you want to know more about related articles, you are welcome to follow the industry information channel.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report