Pull Bastard: Many people have received a variety of invitation code, but few people to study the invitation code is if generated, in the project encountered generate random six-bit invitation code, and the invitation code in the database store, so that is the invitation code can not be repeated.
Idea: I used to generate four invitation codes for example. We can set the source of an array, that is, we define four arrays, keeping each element in each array different, but the same letter can be used in the various arrays. For example: char c1[] = {' A ', ' B ', ' C ', ' D '}; Char c2[] = {' B ', ' C ', ' D ', ' A '}; If you generate a four-bit invitation code, four numbers for each array, you can generate a 4*4*4*4 invitation code, 0-255; consider octal, hexadecimal. The first number is equivalent to 4*4*4, the second 2*64, and the first of the second array is 4*4 and so on.
Junior rookie, if the shortcomings are pointed out, a lot of communication, conducive to growth.
Package com.practice;
Import java.util.ArrayList;
Import Java.util.HashSet;
Import java.util.List;
Import Java.util.Set; Generate six-bit random invitation code public class Randomcode {public static void main (string[] args) {list<string> List = Getinvitecode
(255);
SYSTEM.OUT.PRINTLN (list);
View whether the generated duplicate invitation code set<string> Set = new hashset<string> ();
Set.addall (list);
System.out.println (set.size () = = List.size ()); } * * * list to store size invitation code to ensure that each invitation code is not the same * * public static list<string> getinvitecode (int size) {List<string> ;
List = new arraylist<string> ();
Char c1[] = {' A ', ' B ', ' C ', ' D '};
Char c2[] = {' B ', ' C ', ' D ', ' A '};
Char c3[] = {' C ', ' D ', ' B ', ' A '};
Char c4[] = {' D ', ' C ', ' B ', ' A '};
while (size >= 0) {int temp = size;
StringBuilder sb = new StringBuilder ();
First Invitation code Sb.append (C1[TEMP/(4 * 4 * 4)]);
temp = temp% (4 * 4 * 4);
Second invitation code Sb.append (C1[TEMP/(4 * 4)]);
temp = temp% (4 * 4); Third-bit invitation code Sb.apPend (C1[TEMP/4]);
Temp = temp% 4;
Sb.append (C1[temp]);
List.add (New String (SB));
size--;
} return list;
}
}