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 write the code that generates random numbers in java

2025-01-18 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

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

This article mainly introduces "how to write the code for generating random numbers in java". In the daily operation, I believe that many people have doubts about how to write the code for generating random numbers in java. Xiaobian consulted all kinds of materials and sorted out simple and easy-to-use operation methods. I hope it will be helpful for you to answer the doubts of "how to write the code that generates random numbers in java". Next, please follow the editor to study!

1. When we generate random numbers, we use the method under the Math class: the random () method, whose return value is in [0. 0-1. 0); we set up a for loop to make it more direct, so let's look at the code:

Package DomOne;import java.math.*;public class RanDom {public static void main (String args []) {for (int I = 0; I < 3; iTunes +) {double rand= (Math.random ()); if (iSuppli 0) {System.out.println (the random number of "th" + "th" + "times" is: "+ rand) } else if (item1) {System.out.println (the random number of "th" + I + "th" + "+" is "+ rand");} else {System.out.println (the random number of "th" + I + "th" + "+" is: "+ rand);}

We can see that the output is a number less than 1.

two。 So now let's look at the number of integer random numbers between 100. The code, and comments are as follows:

Import java.math.*;public class RanDom {public static void main (String args []) {for (int I = 0; I < 3; iTunes +) {int rand= (int) (Math.random () * 100room1); / / strongly converts the resulting value to integer if (iLike 0) {System.out.println (random number of "th" + I + "times" is "+ rand") } else if (item1) {System.out.println (the random number of "th" + I + "th" + "+" is "+ rand");} else {System.out.println (the random number of "th" + I + "th" + "+" is: "+ rand);}

Of course, if you want to get different data types, you only need to int rand= (int) (Math.random () * 100data1); replace the int in this string of code with other types, then when you want other range of random numbers, subtract our maximum value from the minimum value range, the integer code is as follows: int num = (int) (Math.random () * (m-n+1) + m).

At this point, the study on "how to write the code to generate random numbers in java" is over. I hope to be able to solve your doubts. The collocation of theory and practice can better help you learn, go and try it! If you want to continue to learn more related knowledge, please continue to follow the website, the editor will continue to work hard to bring you more practical articles!

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