Get the App
SLTechnology News&Howtos  ›  Development  › 

How to write the code that generates random numbers in java

Shulou Source: shulou.com Published: 2022-06-01 12:42:11 09月16日 Update

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!

Tags: Random numbers code middle school students learning methods next more types ranges help different practical maximum minimum middle between data integers integers articles Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Apple MySQL macOS Shulou Tech Info Docker