Java gets a random number between 1-20, a total of 20, requires that cannot be repeated to obtain a random number between 1-20, a total of 10, the requirement cannot be heavy

Source: Internet
Author: User

 PackageCom.swift;ImportJava.util.HashSet;ImportJava.util.Random;ImportJava.util.Set; Public classSuijishu_test { Public Static voidMain (string[] args) {/** Get a random number between 1-20, total 20, requires no weight*/Random ran=NewRandom (); Set<Integer> set =NewHashset<integer>(); intnum;  for(inti = 0; I < 100; i++) {num= Ran.nextint (20) + 1;                SYSTEM.OUT.PRINTLN (num);        Set.add (num);        } System.out.println ();  for(Integer i:set) {System.out.print (i+"~"); }    }}

Get the random number between 1~20 total 10, requires cannot be duplicated

 PackageCom.swift;ImportJava.util.HashSet;ImportJava.util.Random;ImportJava.util.Set; Public classSuijishu_test { Public Static voidMain (string[] args) {/** Get a random number between 1-20, total 10, requires no weight*/Random ran=NewRandom (); Set<Integer> set =NewHashset<integer>(); intnum;  for(inti = 0; I < 100; i++) {num= Ran.nextint (20) + 1;                SYSTEM.OUT.PRINTLN (num);                Set.add (num); if(Set.size () >=10) {                     Break;        }} System.out.println ();  for(Integer i:set) {System.out.print (i+"~"); }    }}

Use a different method to get the random number, System.currenttimemillis ()

 PackageCom.swift;ImportJava.util.HashSet;ImportJava.util.Random;ImportJava.util.Set; Public classSuijishu_test { Public Static voidMain (string[] args) {/** Get a random number between 1-20, total 10, requires no weight*/Set<Integer> set =NewHashset<integer>(); intnum;  for(inti = 0; I < 100; i++) {num=(int) (System.currenttimemillis ()% 20+1); Try{Thread.Sleep (300); } Catch(interruptedexception e) {e.printstacktrace ();            } System.out.println (num);            Set.add (num); if(Set.size () >= 10) {                 Break;        }} System.out.println ();  for(Integer i:set) {System.out.print (i+ "~"); }    }}

Using the random method of the math class

 PackageCom.swift;ImportJava.util.HashSet;ImportJava.util.Random;ImportJava.util.Set; Public classSuijishu_test { Public Static voidMain (string[] args) {/** Get a random number between 1-20, total 10, requires no weight*/Set<Integer> set =NewHashset<integer>(); intnum;  for(inti = 0; I < 100; i++) {num=(int) (Math.random () *20+1);            SYSTEM.OUT.PRINTLN (num);            Set.add (num); if(Set.size () >= 10) {                 Break;        }} System.out.println ();  for(Integer i:set) {System.out.print (i+ "~"); }    }}

Java gets a random number between 1-20, a total of 20, requires that cannot be repeated to obtain a random number between 1-20, a total of 10, the requirement cannot be heavy

Related Article

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.