In addition to Weibo, there is also WeChat
Please pay attention

WeChat public account
Shulou
2025-10-28 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/01 Report--
This article mainly explains "how Java generates random numbers". The content of the explanation is simple and clear, and it is easy to learn and understand. Please follow the editor's train of thought to study and learn how to generate random numbers in Java.
Generate random numbers of integers
Using the nextInt () method of the Random class, you can generate an int type random number that ranges over the entire range of int values.
The nextInt () method can pass in a parameter of type int, and the range of random numbers generated in the case of passing parameters is from 0 to that parameter.
Generate floating-point random numbers
Using the nextFloat () / nextDouble () method of the Random class, you can generate a floating-point random number in the range of 0 to 1.0 (the two methods produce random numbers with different precision).
Other methods of Random
Using the nextGaussian () method of the Random class, you can generate a pseudo-Gaussian distribution with a mean of 0. 0 and a standard deviation of 1. 0 from the random number generator's sequence of double values.
Import java.util.Random;public class RandomNumbers {public static void main (String [] args) {int num1 = new Random (). NextInt (); / / use Random's nextInt method to generate int type random numbers in the range of System.out.println (num1) of the entire int; int num2=new Random () .nextInt (10) / use Random's nextInt method to generate random numbers of type int, ranging from 0 to the value range of input parameters System.out.println (num2); num2 = new Random (). NextInt (10) + 10 new Random / modify the generation range of the final generated random number float num3 = new Random (). NextFloat () / use Random's nextFloat method to generate float type random numbers, floating-point random numbers in the range 0d to 1.0 System.out.println (num3); double num4 = new Random () .nextDouble (); / / use Random's nextDouble method to generate double type random numbers, floating-point random numbers System.out.println (num4) in the range 0d to 1.0 Double num5 = new Random (). NextGaussian (); / / generates a pseudo-Gaussian distribution with a mean value of 0.0 and a standard deviation of 1.0 from the sequence of the random number generator. System.out.println (num5);}}
More skills
You can expand the range of narrowing or moving the range by adding, subtracting, multiplying and dividing.
By adding 10 after the generated random number, you can add 10 to all generated random numbers, thus moving the interval range generated by the random number to the right.
After the generated random number * 10, all the generated random numbers can be expanded by 10 times, thus expanding the range of random numbers generated by 10 times.
Thank you for your reading, the above is the content of "how to generate random numbers in Java". After the study of this article, I believe you have a deeper understanding of how Java generates random numbers, and the specific use needs to be verified in practice. 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.

The market share of Chrome browser on the desktop has exceeded 70%, and users are complaining about
The world's first 2nm mobile chip: Samsung Exynos 2600 is ready for mass production.According to a r
A US federal judge has ruled that Google can keep its Chrome browser, but it will be prohibited from
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope





About us Contact us Product review car news thenatureplanet
More Form oMedia: AutoTimes. Bestcoffee. SL News. Jarebook. Coffee Hunters. Sundaily. Modezone. NNB. Coffee. Game News. FrontStreet. GGAMEN
© 2024 shulou.com SLNews company. All rights reserved.