Java Random.nextint () method

Source: Internet
Author: User

public int Nextint (int n)

The function of this method is to generate a random int value that is in the interval of [0,n], which is a random int value between 0 and N, containing 0 without n.

Directly on the code:

 PackageOrg.xiaowu.random.demo;ImportJava.util.Random;Importorg.junit.Test; Public classRandomdemo {@Test Public voidDemo () {Random rnd=NewRandom (); intCode = Rnd.nextint (8999) + 1000; System.out.println ("Code:" +code); } @Test Public voidDemo1 () {Random R=NewRandom (); intNextint =R.nextint (); Random R1=NewRandom (10); intNextInt2 =R1.nextint (); System.out.println ("Nextint:" +nextint); System.out.println ("NextInt2:" +NextInt2); }        /*** Generate decimals for [0,1.0] intervals **/@Test Public voidDemo2 () {Random R=NewRandom (); DoubleD1 =r.nextdouble (); System.out.println ("D1:" +D1); }        /*** Generate decimals for [0,5.0] intervals **/@Test Public voidDemo3 () {Random R=NewRandom (); DoubleD2 = R.nextdouble () * 5; System.out.println ("D1:" +D2); }        /*** Generate decimals for [1,2.5] intervals **/@Test Public voidDemo4 () {Random R=NewRandom (); DoubleD3 = r.nextdouble () * 1.5 + 1; System.out.println ("D1:" +D3); }        /*** Generate any integer **/@Test Public voidDemo5 () {Random R=NewRandom (); intN1 =R.nextint (); System.out.println ("D1:" +N1); }            /*** integer to generate [0,10] interval **/@Test Public voidDemo6 () {Random R=NewRandom (); intN2 = R.nextint (10); intN3 = Math.Abs (R.nextint ()% 10); System.out.println ("N2:" +n2); System.out.println ("N3:" +N3); }            /*** Generate integers for [0,10] intervals **/@Test Public voidDemo7 () {Random R=NewRandom (); intN3 = R.nextint (11); intN4 = Math.Abs (r.nextint ()% 11); System.out.println ("N3:" +N3); System.out.println ("N4:" +N4); }        /*** Generate integer for [ -3,15] interval **/@Test Public voidDemo8 () {Random R=NewRandom (); intN4 = R.nextint (18)-3; intN5 = Math.Abs (R.nextint ()% 18)-3; System.out.println ("N4:" +N4); System.out.println ("N5:" +N5); }    }

Java Random.nextint () method

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.