In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-16 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/01 Report--
This article mainly explains the "Java how to generate random integers in the specified range", the content of the article is simple and clear, easy to learn and understand, the following please follow the editor's ideas slowly in depth, together to study and learn "Java how to generate random integers in the specified range" bar!
1. Java.Math.random ()
When it comes to randomness, Math.random () comes to mind in many people's minds. Instead of generating an integer directly, it generates a decimal of type double between [0.0,1.0), as follows:
Public class demo01 {public static void main (String [] args) {System.out.println (Math.random ()); System.out.println (getType (Math.random ();} public static String getType (Object obj) {return obj.getClass () .toString ();}}
Print the results:
0.9567296616768681
Class java.lang.Double
Thus, this method can only generate a random double type decimal between [0. 0. 0. 0. 0. 0). So how to use the random method to generate random decimals within a specified range?
Suppose we need to generate a random integer in the range of [1 # 10]. The specific idea is as follows.
Math.random () = > [0.0,1.0) Math.random () * 10 = = > [0.0,10.0) (int) (Math.random () * 10) = = > [0,9] (int) (Math.random () * 10) + 1 = = > [1,10] for (int I = 0; I
< 10; i++) { int a=(int)(Math.random() * 10 )+1; System.out.println(a);} 最后打印结果(多次结果): 9 10 1 10 3 1 6 8 7 5 可见结果符合我们的要求。 2.Java.util.Random.nextInt();Random random=new Random();int a=random.nextInt(25); // 在[0, 25)之间随机生成一个 int 类型整数 假设我们需要生成一个 [63, 99]之间的整数,具体实现思路如下: [63, 99] ===>First find the maximum common multiple of these two numbers = = > 9 [7,11] * 9 = = > take the minimum value 0 ([0,4] + 7) * 9
Random random=new Random (); for (int I = 0; I < 10; iTunes +) {int a = (random.nextInt (5) + 7) * 9; System.out.println (a);}
Print the results:
seventy-two
eighty-one
ninety-nine
ninety
sixty-three
ninety-nine
sixty-three
seventy-two
eighty-one
ninety-nine
Thank you for your reading, the above is the content of "how to generate random integers in the specified range by Java". After the study of this article, I believe you have a deeper understanding of how to generate random integers in the specified range by Java. Here is, the editor will push for you more related knowledge points of the article, welcome to follow!
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.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.