Info

This question is closed. Reopen it to edit or answer.

how you could adjust this script to guarantee that there is at least one letter and at least one digit?

1 view (last 30 days)
N=input('Give length of password to be generated: ');
characters='ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789';
[m,n]=size(characters);
for i=1:N
password(i)=characters(randi([1 n],1,1));
end
disp('Your password is: ')
disp(password)

Answers (3)

Joseph Cheng
Joseph Cheng on 2 Sep 2015
generate a alpha numeric template like:
N=20
minreq = 0;
while ~minreq
alphanum = randi(2,1,N)
minreq = any(alphanum==1)&any(alphanum==2)
end
alphanum'
then substitute in the forloop a letter or number depending on the template.
  1 Comment
H
H on 2 Sep 2015
I haven't leant any of these command which you have used. is it possible to use for-loop, if-statement or string instead of the commands you have used?

Walter Roberson
Walter Roberson on 2 Sep 2015
hasletter = false;
hasdigit = false;
while ~hasletter || ~hasdigit
for ..... %generate password
end
hasletter = false;
for i = 1 : N
for Lidx = 1 : 52
if password(i) == Letters(Lidx)
hasletter = true;
end
end
end
hasdigit = false;
for i = 1 : N
for Lidx = 53 : length(Letters)
if password(i) == Letters(Lidx)
hasdigit = true;
end
end
end
end
I would never use this code in practice, but it only uses parts of MATLAB you already know how to use.

John D'Errico
John D'Errico on 2 Sep 2015
Trivial, though I'm not sure why nobody has suggested this. No rejection required, nothing complicated.
Since the final result MUST have at least one letter, and at least one digit, generate them up front. Then generate the rest of the string randomly from the entire set, and then randomly permute the result.
N=input('Give length of password to be generated: ');
characters='ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789';
str = repmat(' ',1,N);
str(1) = characters(randi([53,62])); % 1 digit
str(2) = characters(randi([1,52])); % 1 alpha char
str(3:N) = characters(randi([1,62],1,N-2)); % remainder
str = str(randperm(N)); % permute
For example, with N == 10, here is one result:
str
str =
80OJx77he7
  5 Comments
Roger Stafford
Roger Stafford on 4 Sep 2015
@John. I assume that you did the final random permutation to ensure that the selection is made randomly among all possible valid passwords. However, I don't think it quite achieves that objective. To make things simpler for a counterexample suppose we only require that there be at least one digit, that there are 4 possible letters and 3 possible digits to choose from, and that n is equal to 2. Now divide up all possible passwords into two classes, the first being that the first character is not a digit, and the second class where it is.
By my calculations your algorithm would choose a first class selection with probability 2/7 and probability 5/7 for the second class. However, there are 7^2-4^2 = 33 different pairs altogether of characters that satisfy these requirements, and of these there are only 4*3 = 12 that are of the first class which gives a probability of 12/33 = 4/11, leaving the second class with a probability of 7/11. These two sets of probabilities don't agree.
I have reason to believe that an algorithm that avoids rejection and achieves the correct probabilities would be considerably more difficult.
John D'Errico
John D'Errico on 4 Sep 2015
@Roger - no requirement was made that the solution must select passwords with perfect uniformity. As well, it is not the final random permutation that is at issue. If the passwords have elements in a randomly chosen sequence, then any number of final random permutations will have no impact.
I'll argue the issue is that in your counter-example, the passwords '1a' and 'a1' should each have the same probability of occurrence as the password '11', yet my algorithm would not generate sets of passwords with that property.
Next, consider the case for n = 2, where both a letter and number are required. Here my algorithm is perfect. Even for the counter-example you give, the probabilities are not that far off.
As for the case posed by the OP, where we start with the universe set of:
characters='ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789';
then my algorithm is quite reasonable.

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!