Generate a random number using JavaSE.

Source: Internet
Author: User

Generate a random number using JavaSE.

Today, the instructor talked about how to use jvm to implement random numbers (I am very interested in this). A simple code for random integers less than 100 is as follows:

/** Generate random number */import java. util. random; public class Random01 {int I; public static void main (String [] args) {// create a Random number generator Random r = new Random (); // generate a random number of the int type // int I = r. nextInt (101); // Random Number of [0-100] // System. out. println (I); for (int I = 0; I <5; ++ I) {System. out. println (r. nextInt (101 ));}}}
/** Generate random number */import java. util. random; public class Random01 {int I; public static void main (String [] args) {// create a Random number generator Random r = new Random (); // generate a random number of the int type // int I = r. nextInt (101); // Random Number of [0-100] // System. out. println (I); for (int I = 0; I <5; ++ I) {System. out. println (r. nextInt (101 ));}}}

Then the teacher assigned a new requirement: Write a random integer of less than 100, and it cannot be repeated. After thinking about it, write it like this:

import java.util.*;public class suiji{    public static void main(String[] args){        Random aa = new Random();        ArrayList list = new ArrayList();        for(int i=0; i< 5 ; ++i){            list.add(aa.nextInt(101));            for(int k=0; k < i; ++k){                if(list.get(i).equals(list.get(k))){                    list.remove(i);                    i--;                }            }            System.out.println( aa.nextInt(101) );        }    }}
import java.util.*;public class suiji{    public static void main(String[] args){        Random aa = new Random();        ArrayList list = new ArrayList();        for(int i=0; i< 5 ; ++i){            list.add(aa.nextInt(101));            for(int k=0; k < i; ++k){                if(list.get(i).equals(list.get(k))){                    list.remove(i);                    i--;                }            }            System.out.println( aa.nextInt(101) );        }    }}

Please make an axe.

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ ~~~~~~~~~~ Jie Li Education

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.