Three algorithms are used to generate random numbers in Java.
1. Math. Random () // generates a random number between 0 and 1. The type is double.
2. New random ()
Random. nextinextint (100) // generates a random number between 0 and. The type is int.
3. system. currenttimemillis () // returns the number of milliseconds that have elapsed since 00:00:00, January 1, January 1, 1970. The type is long.
The Code is as follows:
Package COM. njupt. ACM; import Java. util. random; public class test1 {public static void main (string [] ARGs) {generaterandomnumber3 ();} public static void generaterandomnumber1 () {random r = new random (); for (INT I = 0; I <100; ++ I) {if (I % 10 = 0 & I! = 0) {// change the system for every 10 rows. out. println ();} system. out. print ("" + R. nextint (100); // R. nextint (100) generates a number between [0,100)} public static void generaterandomnumber2 () {for (INT I = 0; I <100; ++ I) {if (I % 10 = 0 & I! = 0) {system. out. println ();} system. out. print (math. random () * 100); // math. random () * 100 generates 0 ~ A Random Number of the double type between 100} public static void generaterandomnumber3 () {// system. currenttimemillis () returns a millisecond value from 00:00:00, January 1, January 1, 1970 // to the current time (Note: The number of milliseconds instead of the number of seconds). The return type is Longlong nowtime = system. currenttimemillis (); long tianshu = (nowtime/1000/60/60/24); system. out. println ("the number of milliseconds that have elapsed since 00:00:00, January 1, January 1, 1970 is:" + nowtime); system. out. println ("the number of years from January 1, 1970 00:00:00 to the present is:" + (tianshu/365 ));}}