Java implements random and non-repeated numbers.
Generally, developers with some development experience can achieve this function, but it is only a matter of efficiency. In general, when we are faced with such a problem, we will always think of it in a straight order. Mr will form an array and then add random numbers to the array in a loop, when you add a number, first check whether the number exists in the array. If the number does not exist, add it to the array. If the number exists, no value is added. We generally think about the problem in this way, so we can also implement the function. I just mentioned that it is just a matter of efficiency.
To better understand this question, let's first look at the specific content: generate a random array of 1-100, but the numbers in the array cannot be repeated, that is, the positions are random, but the array elements cannot be repeated. Here, we do not specify the length of the array. We can make it an arbitrary length between 1 and.
Next, let's take a look at how to implement it better. We usually use ArrayList to implement it, as shown in the following code:
Copy codeThe Code is as follows:
Package cn. sunzn. randomnumber;
Import java. util. ArrayList;
Import java. util. Random;
Public class Demo {
Public static void main (String [] args ){
Object [] values = new Object [20];
Random random = new Random ();
ArrayList <Integer> list = new ArrayList <Integer> ();
For (int I = 0; I <values. length; I ++ ){
Int number = random. nextInt (100) + 1;
If (! List. contains (number )){
List. add (number );
}
}
Values = list. toArray ();
/********* Traverse the array and print data **********/
For (int I = 0; I <values. length; I ++ ){
System. out. print (values [I] + "\ t ");
If (I + 1) % 10 = 0 ){
System. out. println ("\ n ");
}
}
}
}
The above implementation process is relatively inefficient. The time complexity is O (N ^ 2 ). We can think about the functions of HashSet and HashMap in this way. HashSet implements the Set interface. In mathematics, a Set is defined as a Set without repetition or order. HashMap implements Map, and duplicate keys are not allowed. In this way, we can use HashMap or HashSet.
When using HashMap, you only need to convert its key into an array. The Code is as follows:
Copy codeThe Code is as follows:
Package cn. sunzn. randomnumber;
Import java. util. HashMap;
Import java. util. Random;
Public class Demo {
Public static void main (String [] args ){
Object [] values = new Object [20];
Random random = new Random ();
HashMap <Object, Object> hashMap = new HashMap <Object, Object> ();
/******* Generate random numbers and store them into HashMap *******/
For (int I = 0; I <values. length; I ++ ){
Int number = random. nextInt (100) + 1;
HashMap. put (number, I );
}
/********** Import arrays from HashMap **********/
Values = hashMap. keySet (). toArray ();
/********** Traverse the array and print data ***********/
For (int I = 0; I <values. length; I ++ ){
System. out. print (values [I] + "\ t ");
If (I + 1) % 10 = 0 ){
System. out. println ("\ n ");
}
}
}
}
Because the relationship between HashSet and HashMap is too close, HashSet is implemented by HashMap at the underlying layer, except that there is no set of values and there is only one set of keys, so HashSet can also be implemented, the Code is as follows:
Copy codeThe Code is as follows:
Package cn. sunzn. randomnumber;
Import java. util. HashSet;
Import java. util. Random;
Public class Demo {
Public static void main (String [] args ){
Random random = new Random ();
Object [] values = new Object [20];
HashSet <Integer> hashSet = new HashSet <Integer> ();
/******* Generate random numbers and store them in HashSet *******/
For (int I = 0; I <values. length; I ++ ){
Int number = random. nextInt (100) + 1;
HashSet. add (number );
}
Values = hashSet. toArray ();
/********** Traverse the array and print data **********/
For (int I = 0; I <values. length; I ++ ){
System. out. print (values [I] + "\ t ");
If (I + 1) % 10 = 0 ){
System. out. println ("\ n ");
}
}
}
}
This is more efficient. If we limit the length of the array, we only need to change the for loop and set it to a whlie loop. As follows:
Copy codeThe Code is as follows:
Package cn. sunzn. randomnumber;
Import java. util. HashSet;
Import java. util. Random;
Public class Demo {
Public static void main (String [] args ){
Random random = new Random ();
Object [] values = new Object [20];
HashSet <Integer> hashSet = new HashSet <Integer> ();
/****** Generate random numbers and store them in HashSet ******/
While (hashSet. size () <values. length ){
HashSet. add (rand.nextint (100) + 1 );
}
Values = hashSet. toArray ();
/********* Traverse the array and print data **********/
For (int I = 0; I <values. length; I ++ ){
System. out. print (values [I] + "\ t ");
If (I + 1) % 10 = 0 ){
System. out. println ("\ n ");
}
}
}
}