Java Basic Knowledge Hardening 83:random class overview and method usage

Source: Internet
Author: User

1. Random class

1  Public class Random  extends  implements  Serializable;

An instance of this class is used to generate a pseudo-random number stream. This class uses 48-bit seeds.

(1) Random class overview

• This class is used to generate random numbers

• If you create two random instances with the same seed, the same method call sequence is made for each instance, and they will generate and return the same sequence of numbers.

(2) How random is constructed

Public Random (): No seed is used, is the default seed, is the millisecond value of the current time

• Public Random (Long Seed): to a fixed seeding seed

Seed: The random number of a general computer is a pseudo-random number, with a true random number (seed) as the initial condition, and then iterative generation of random numbers with a certain algorithm.

(3) Random member method

1  Public  int   nextint ():            Returns a random number in the range of int  2public  int  Nextint (int n):      Returns a random number in the range of [0,n]

(4) Code example:

1  Packagecn.itcast_01;2 3 ImportJava.util.Random;4 5 /*6 * Random: A class that generates random numbers7  * 8 * Construction Method:9 * Public Random (): No seed, with default seed, is the millisecond value of the current timeTen * Public Random (long Seed): gives the specified seed One  * A * Once the seed is given, the random number obtained is the same each time.  -  * - * Member Method: the * public int nextint (): Returns a random number in the range of int - * public int nextint (int n): Returns the internal random number of the range of [0,n] -  */ -  Public classRandomdemo { +      Public Static voidMain (string[] args) { -         //Creating Objects +         //random R = new Random (); ARandom r =NewRandom (1111); //Given seed, the random number returned each time is the same  at  -          for(intx = 0; x < 10; X + +) { -             //int num = R.nextint (); -             intnum = r.nextint (100) + 1; - System.out.println (num); -         } in     } -}

Java Basic Knowledge Hardening 83:random class overview and method usage

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.