EvilZone
Programming and Scripting => .NET Framework => : bubzuru October 08, 2013, 01:01:00 AM
-
i needed this for a project im working on, its not the best but it kinda works
public static string ProRandName(Random RandNumGen, int Len)
{
char[] consts = {'b', 'c', 'd', 'f', 'g', 'h', 'j', 'k', 'l', 'm', 'n',
'p', 'r', 's', 't', 'v', 'w', 'x', 'y','z', 'B', 'C', 'D',
'F', 'G', 'H', 'J', 'K', 'L', 'M', 'N', 'P', 'R', 'S', 'T', 'V', 'W', 'X', 'Y','Z' };
char[] vowls = { 'a', 'e', 'i', 'o', 'u', 'A', 'E', 'I', 'O', 'U' };
bool cswitch = Convert.ToBoolean(RandNumGen.Next(0, 2));
string final = "";
for (int i = 0; i <= Len; i++)
{
if (cswitch == true)
{
final += consts[RandNumGen.Next(1, 41) - 1];
cswitch = false;
}
else
{
final += vowls[RandNumGen.Next(1, 11) - 1];
cswitch = true;
}
}
return final;
}
-
I modified your code a little:
/// <summary>
/// Generate a pronounceable pseudorandom string.
/// </summary>
/// <param name="r">Random number generator to use.</param>
/// <param name="len">Length of the returned string.</param>
public static string GeneratePronounceableRandomString(Random r, int len)
{
char[] consonants = {'b', 'c', 'd', 'f', 'g', 'h', 'j', 'k', 'l', 'm', 'n', 'p', 'r', 's', 't', 'v', 'w', 'x', 'y', 'z'};
char[] vowels = {'a', 'e', 'i', 'o', 'u'};
StringBuilder word = new StringBuilder();
bool consonant = r.Next(0, 1) == 1;
for (int i = 0; i < len; i++)
{
word.Append(consonant ? consonants[r.Next(0, consonants.Length)] : vowels[r.Next(0, vowels.Length)]);
consonant = !consonant;
}
return word.ToString();
}
-
I modified your code a little:
/// <summary>
/// Generate a pronounceable pseudorandom string.
/// </summary>
/// <param name="r">Random number generator to use.</param>
/// <param name="len">Length of the returned string.</param>
public static string GeneratePronounceableRandomString(Random r, int len)
{
char[] consonants = {'b', 'c', 'd', 'f', 'g', 'h', 'j', 'k', 'l', 'm', 'n', 'p', 'r', 's', 't', 'v', 'w', 'x', 'y', 'z'};
char[] vowels = {'a', 'e', 'i', 'o', 'u'};
StringBuilder word = new StringBuilder();
bool consonant = r.Next(0, 1) == 1;
for (int i = 0; i < len; i++)
{
word.Append(consonant ? consonants[r.Next(0, consonants.Length)] : vowels[r.Next(0, vowels.Length)]);
consonant = !consonant;
}
return word.ToString();
}
nicebro will you comment the code for me so i can understand properly
-
nicebro will you comment the code for me so i can understand properly
Not much to comment really. Only things I changed were:
1) String to StringBuilder for performance reasons (Google it).
2) i <= len to i < len because the former would run len + 1 times (thus producing a string of len + 1).
3) If statement to a ternary if (basically the same thing with the exception of the ternary having to return something).
Of course that doesn't include obvious things like boolean stuff changes.
-
I'm going to mention changing those char arrays to strings, because they can be indexed just like a char array, without changing the rest of the code. :) Still contiguous in memory, and probably much easier to read as well. A string is basically just an array of characters in .NET, but the string datatype also encapsulates many useful methods of dealing with those characters.
I'd just pass the StringBuilder as the return value to the caller as well. This allows it so that if the user wants to format that string further, they don't have to put it back into a StringBuilder instance, or concatenate with regular strings. They can cast to a string if they want too.
The last thing I would mention doing, since you know the length by the len parameter which is passed to the function, is to set the capacity of the StringBuilder instance by utilizing the constructor that specifies a capacity. This eliminates possible reallocations to adjust the size of the StringBuilder unnecessarily.