[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
RE: Repeating bug (error: subscript indices must be either positiveinteg
From: |
Allen.Windhorn |
Subject: |
RE: Repeating bug (error: subscript indices must be either positiveintegers or logicals) |
Date: |
Wed, 27 Apr 2011 15:15:35 -0500 |
From:
address@hidden On Behalf Of ragan2328
> I've had yet another problem dealing with an
infinite loop I was
> hoping you could help out with. I have a feeling
this one is a quick
> fix. Additionally, the details of the assignment
were to create a
> generator that outputted a password, but that it must
have at least
> one uppercase letter, one lowercase letter, one digit, and
the first
> character must be a letter.
> Now, the problem I've been having was that the
password never seems
> to pass the requirements in PassCheck, resulting in
an infinite
> loop. I'm reattaching my revised programs, I hope you can
help me
> out on this one. Thanks so much
again.
I didn't look for
your bug, but you might try a different approach. I
made
a similar password
generator, but what I would recommend is to first
generate a password
of (say) n all lower-case letters. Randomly
change the case of
the first letter with a probability of 0.5:
lower1 =
1;
if (rand() >
0.5)
password(1) =
toupper(password(1);
lower1 =
0;
end
Take the
next k
characters where k is a random integer between 1
and
(n-3) and
change them
to random digits:
n =
length(password);
k =
floor(rand()*(n-3)+1);
digs =
int2str(floor(10*rand(k,1)));
password(2:k+1) =
digs;
Maybe the value of k
should be biased or limited to a lower number since
there aren't as
many digits
as letters.
Take the
last
(n-k-1) characters and change a random number of them
to upper
case (but
not all, unless the first character is lower case):
m =
floor(rand()*(n-k-2+lower1));
password(k+2:k+2+m)
= toupper(password(k+2:k+2+m);
I haven't tested this exhaustively so it wants some
careful counting in
case I'm off by one somewhere. m should range
from 0 to two less
than the remaining number of letters, or one less if
the first letter is lower
case.
Then scramble the
last n-1 characters:
password(2:n) =
shuffle(password(2:n));
I
wrote a
shuffle function below but there is probably already one out
there somewhere.
function chars =
shuffle(str)
%shuffle(str) Return shuffled string
% Randomly permute
characters of string and return shuffled version
%
chars =
str;
for idx = 1:length(str)
swp =
randpick(1:length(str));
tmp =
chars(idx);
chars(idx) = chars(swp);
chars(swp) = tmp;
end
endfunction
%
function k =
randpick(rng)
% Select a random integer within range
k =
floor(rand()*length(rng)+1);
endfunction
%
Regards,
Allen