Java Random Number Generation Algorithm

Source: Internet
Author: User

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 ));}}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.