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 use Math class and Random class in Java

2025-03-31 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

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

Today, the editor will share with you the relevant knowledge points about how to use Math class and Random class in Java. The content is detailed and the logic is clear. I believe most people still know too much about this knowledge, so share this article for your reference. I hope you can get something after reading this article. Let's take a look at it.

Math class of java

The java.lang.Math class in java can be used directly without a guide package. It is seen in the API of JDK that the Math class is decorated by final, which means that the Math class cannot be inherited, the constructor is privatized, and objects of the Math class cannot be created, that is to say,

Public static void main (String [] args) {Math m = new Math ();}

This way of writing is wrong.

All properties and methods inside Math are modified by static, which means that the class name is used directly when calling. Without the need to create an object

Next, we will introduce several common methods in the Math class:

Method description return value type random () returns a double-precision value with a positive sign, greater than or equal to 0. 0 and less than 1. 0. The absolute value of static doubleabs () can be int long double floatint / long / double / floatceil () return a double type floor () up value return a double type round () rounding return a long or values of int type max () and min () return maximum and minimum values can be values of double, float, int, long types package Test Public class Test06 {public static void main (String [] args) {/ / Common attribute: System.out.println (Math.PI); / / commonly used method: System.out.println ("Random number:" + Math.random ()); / / [0.0Cool 1.0) System.out.println ("absolute value" + Math.abs (- 80)) System.out.println ("pick up" + Math.ceil (9.1); System.out.println ("pick down" + Math.floor (9.9)); System.out.println ("rounded" + Math.round (3.5); System.out.println ("take the larger value" + Math.max (3,6)) System.out.println ("take the smaller value" + Math.min (3,6));}}

Similarly, the Math class can import all the methods and properties of the Math class directly using static import.

Package Test;// static import import static java.lang.Math.*;public class Test07 {public static void main (String [] args) {/ / Common attribute: System.out.println (PI); / / commonly used method: System.out.println ("Random number:" + random ()); / / [0.0Cool 1.0) System.out.println ("absolute value" + abs (- 80)) System.out.println ("pick up" + ceil (9.1); System.out.println ("pick down" + floor (9.9)); System.out.println ("rounded" + round (3.5)); System.out.println ("take the larger value" + max (3,6)); System.out.println ("take the smaller value" + min (3,6)) } / / if the method in Math is repeated, the program will give priority to the method of this class, which is the Random class of the nearest principle public static int random () {return 100;}} java

The Random class is in java.util.Random

Common methods of the Random class:

The method explains that nextInt () returns the next pseudo-random, uniformly distributed int value in this random number generator sequence. NextDouble () returns the next pseudo-random, uniformly distributed double precision value between 0 and 1 in this random number generator sequence. Package Test;import java.util.Random;public class Test08 {public static void main (String [] args) {System.out.println ("random number" + Math.random ()); / / learn Random classes / / create objects Random R1 = new Random (System.currentTimeMillis ()) using constructors with parameters; int I = r1.nextInt (); System.out.println (I) / / create the object Random R2 = new Random () using the empty parameter constructor; / / the surface is calling the non-parameter constructor, but in fact, the underlying layer still calls the parameter constructor System.out.println (r2.nextInt (10)); System.out.println (r2.nextDouble ()) }} these are all the contents of the article "how Math classes and Random classes in Java are commonly used". Thank you for reading! I believe you will gain a lot after reading this article. The editor will update different knowledge for you every day. If you want to learn more knowledge, please pay attention to the industry information channel.

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