Requirement: Get 10 random numbers between 1-20 and require no repetition
/**
Analysis
*A: Create an object that produces a random number
*B: Create a collection that stores random numbers.
*C: Define a statistical variable. Starting from 0.
*D: Determine if the statistical traversal is less than 10
*is: A random number is generated to determine whether the random number exists in the collection.
*If it does not exist: Add, Statistics variable + +.
*If there is: do not respond to it.
*No: Ignore it.
*E: Iterating through the collection
*/
<span style= "FONT-SIZE:18PX;" >public class Settest {public static void main (string[] args) {Random random = new random (); int count = 0; list<integer> list = new arraylist<integer> (), while (count<10) {int randomnumber = Random.nextint ( +1;if (!list.contains (Randomnumber)) {List.add (randomnumber); count++;}} for (int i=0;i<list.size (); i++) {System.out.println (List.get (i));}}} </span>
This is the use of the list collection, if you have a set set of features that cannot be repeated, then I do not need to add to the collection of data to judge
<span style= "FONT-SIZE:18PX;" >public class Settest {public static void main (string[] args) {Random random = new random (); hashset<integer> set = new Hashset<integer> (), while (Set.size () <10) {int randomnumber = Random.nextint (+1;set.add) (Randomnumber);} for (Integer i:set) {System.out.println (i);}}} </span>
Gets 10 random numbers between 1-20 and requires no repetition