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 generate random numbers between arbitrary numbers in java

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

Share

Shulou(Shulou.com)06/03 Report--

This article is about how to generate random numbers between arbitrary numbers in java. The editor thinks it is very practical, so share it with you as a reference and follow the editor to have a look.

Public static int cssjs (int a drawing int b) {Random rand=new Random (); int zhi; zhi=rand.nextInt (b)% (b-a+1) + a; return zhi;}

We observe the nextInt (int) method of its Random object and find that this method generates integers with random values between 0 and parameters. For example (suppose there is Random rand = new Random ()

Rand.nextInt (100); this line of code will generate a random number in the range 0 to 100. Interestingly, the value may be 0, but it cannot be 100. We use the interval representation of middle school math class, which is expressed as: [0100).

The explanation of the mathematical principle of generating random numbers in any range of values: first get the random number between 0-max, and then take the remainder of max-min+1, so that the result is 0 to max-min, and then add the minimum value min, which is the required random number. Suppose max = 100 leading mines. nextInt (100) should be a number between 0 and 100, and max-min+1 should be 51.

The number obtained by taking the remainder should be 0-50. Finally, the minimum number (50) should be 50-100. It corresponds to the assumed maximum and minimum numbers. You can assume other numbers to try.

Thank you for reading! This is the end of this article on "how to generate random numbers between arbitrary numbers in java". I hope the above content can be of some help to you, so that you can learn more knowledge. if you think the article is good, you can share it for more people to see!

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