Author Topic: [C# Snippet] Random Pronounceable Word  (Read 2461 times)

0 Members and 3 Guests are viewing this topic.

Offline bubzuru

  • Knight
  • **
  • Posts: 395
  • Cookies: 21
  • everything is contained in the data
    • View Profile
    • New School Tools
[C# Snippet] Random Pronounceable Word
« on: October 08, 2013, 01:01:00 am »
i needed this for a project im working on, its not the best but it kinda works

Code: [Select]
        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;
        }
« Last Edit: October 08, 2013, 01:01:20 am by bubzuru »
Damm it feels good to be gangsta
http://bubzuru.comule.com

Offline Fur

  • Knight
  • **
  • Posts: 216
  • Cookies: 34
    • View Profile
Re: [C# Snippet] Random Pronounceable Word
« Reply #1 on: October 08, 2013, 01:29:15 am »
I modified your code a little:
Code: (CSharp) [Select]
/// <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();
}
« Last Edit: October 08, 2013, 02:20:37 am by Fur »

Offline bubzuru

  • Knight
  • **
  • Posts: 395
  • Cookies: 21
  • everything is contained in the data
    • View Profile
    • New School Tools
Re: [C# Snippet] Random Pronounceable Word
« Reply #2 on: October 08, 2013, 09:31:13 pm »
I modified your code a little:
Code: (CSharp) [Select]
/// <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
Damm it feels good to be gangsta
http://bubzuru.comule.com

Offline Fur

  • Knight
  • **
  • Posts: 216
  • Cookies: 34
    • View Profile
Re: [C# Snippet] Random Pronounceable Word
« Reply #3 on: October 09, 2013, 12:00:45 am »
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.

Offline ArkPhaze

  • Peasant
  • *
  • Posts: 136
  • Cookies: 20
  • null terminated
    • View Profile
Re: [C# Snippet] Random Pronounceable Word
« Reply #4 on: November 01, 2013, 02:07:04 am »
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.
« Last Edit: November 01, 2013, 02:12:10 am by ArkPhaze »
sig=: ArkPhaze

[ J/ASM/.NET/C/C++ - Software Engineer ]