This method creates a 16-character PAC consisting of only digits 0-9 and upper- and lowercase letters
This works, but I'm wondering if someone has a better way. Specifically, this has the possibility of
throwing chains of identical characters ie:
4j64xYYvk2 - 2 Y's
6i4xYYY7k4 - 3 Y's
u3bNNNN5q - 4 N's
how could the method be massaged to ensure there are no chains?
As usual, all comments, suggestions, positive criticism and especially examples are all welcome.
Off the top of my head I can think of two ways to deal with that:
(1) Don't worry about it (why is it a problem anyway?)
(2) If you get a code which flunks the no-consecutive-repeats rule, throw it out and get another one. Repeat until satisfied.
(3) ... okay I'm thinking as I type, it looks like... If you get a code which flunks, find the guilty characters and replace them by some other character which allows the code to pass.
Stuart Rogers wrote:how could the method be massaged to ensure there are no chains?
That is easy - if the character you've just generated is the same as the previous one, throw it away and get a new one. Eventually a different character must come up, otherwise your random generator would not deserve to be called random.
The hard part is - how is your application going to verify the given activation code is valid?
Another suggestions: stick to the numbers and upper-case letters and remove any numbers/letters that could be mistaken for another one (0 and O, 1 and I and so on). It helps the user tremendously if he needs to verify that the code has been entered correctly. A set of 32 characters I once used for this kind of functionality was 23456789ABCDEFGHJKLMNPQRSTUVWXYZ. If you plan to supply the code solely in a digital form, it is not as important.