Code is pretty bad. Put all your "possibles" in one string. Do not catch exception! This is pointless and will give you a nightmare when you debug code. It it it possible to define possibles just in the form of range (two variables: minimum and maximum code point), would be even easier, as your would simply convert a randomly-generated integer to a character, not using indexes, which is better for performance as you would eliminate exactly one step of indirection.
(See my directions on using exceptions here:
How do i make a loop that will stop when a scrollbar reaches the bottom[
^]
When i run an application an exception is caught how to handle this?[
^])
In the loop, use 0 to
<length
, not
<=length
, use
k++
.
Use longer variable names, never of one character. Imagine how you do the search. Don't use names ending with 1, 2, etc., rename all auto-generated names to somethings semantic. Use Microsoft naming conventions.
Use only one instance of
Random
, initialize it outside of your method, only once per application run-time.
Use
Random.Next
with one parameter:
maxValue
, because it's always an index. Don't make a mistake in
maxValue
: it's not
String.Length
but
String.Length - 1
.
—SA