A method of implementing random and no repetition numbers in Java _java

Source: Internet
Author: User

It is only a matter of efficiency that a friend who has developed experience can achieve such a function. We generally in the face of such problems, always tile the direct sequence of association, mister into an array, and then add a random number to the array in a loop, and in the process of adding a number, look for the number in the array and add it directly to the array if it doesn't exist, if that number is not added. We generally consider the problem in this way, so that consideration can also realize the function, as I said just now, it is only a matter of efficiency.

To better understand this, let's take a look at the details: Generate a random array of 1-100, but the numbers in the array cannot be repeated, that is, the position is random, but the array elements cannot be duplicated. Here, without giving us the length of the array, we can let it be any length between 1-100.

Let's take a look at how to implement it better, usually using ArrayList, as shown in the following code:

Copy Code code 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 the 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 less efficient. Because every time you add it, go through the current list to see if this number exists, and the time complexity is O (n^2). We can think about this: since there is no duplication involved, we can think about the functions of HashSet and HASHMAP. HashSet implements the set interface, and set is mathematically defined as a collection of no repetition, no order. and HASHMAP implementation of MAP, is not allowed to duplicate Key. This enables us to use HASHMAP or hashset.

When using the HASHMAP implementation, just convert its key to an array, and the code is as follows:

Copy Code code 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> ();

/******* generates random numbers and deposits 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 the 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 at the bottom is to use HASHMAP to achieve, but there is no Value of the collection, only a set of Key, so you can use the HashSet to implement, the code is as follows:

Copy Code code 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> ();

/******* generates random numbers and deposits 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 the 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 a little more efficient. If we limit the length of the array, we just need to transform the for loop and set it to the Whlie loop. As shown below:

Copy Code code 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> ();

/****** generates random numbers and deposits HashSet ******/
while (Hashset.size () < Values.length) {
Hashset.add (Random.nextint (100) + 1);
}

Values = Hashset.toarray ();

/********** traverse the array and print the data **********/
for (int i = 0; i < values.length; i++) {
System.out.print (Values[i] + "T");
if ((i + 1)% 10 = 0) {
System.out.println ("\ n");
}
}
}
}

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.