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