Info
此问题已关闭。 请重新打开它进行编辑或回答。
how you could adjust this script to guarantee that there is at least one letter and at least one digit?
2 次查看(过去 30 天)
显示 更早的评论
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)
0 个评论
回答(3 个)
Joseph Cheng
2015-9-2
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 个评论
Walter Roberson
2015-9-2
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.
1 个评论
John D'Errico
2015-9-2
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 个评论
Roger Stafford
2015-9-4
@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
2015-9-4
@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!