Reputation: 5691
I'm working on a random password generator to create passwords that meet certain conditions including, but not necessarily limited to:
What would be the best algorithmic approach to ensure that the generated password meets all these?
I'm not looking for a complete solution, I only need a few good ideas and guidelines.
Upvotes: 5
Views: 9540
Reputation: 64740
There is an alternative to the precise construction proposed by Armen. If you're conditions can be met with high probability then:
In a lazy language this is about 6 lines of non-boilerplate code and doesn't require any shuffling.
EDIT: Yes, step a window as in the comments, not slide a window. Thanks!
Upvotes: 1
Reputation: 18155
The algorithm should usually succeed on the first few iterations, and saves you from having to implement a shuffle algorithm.
Upvotes: 4
Reputation: 133072
1) randomly generate number L which will be the exact length of your password. Namely, generate is so that it is greater than 8
2) randomly generate a number LL which will be the number of lowercase letters. LC must be in range [1..L-2]
3) randomly generate number LU for uppercase. Must be in range [1..L-LL-1]
4) LD = L-LL-LU number of uppercase digits
5) randomly generate LL lowercase letters, LU uppercase letters, and LD digits and keep them in a list(array)
6) Shuffle the array randomly
hth
Upvotes: 5