Discuss Scratch
- Discussion Forums
- » Advanced Topics
- » Random name generator in C#
- DerAxeEffekt
- Scratcher
100+ posts
Random name generator in C#
I have tried to make a random name generator using C#. It built without errors, but it still doesn't work (When trying to print out some names, it prints nothing.).
Here's the function so far:
Sorry for possible grammar errors.
Here's the function so far:
public static string generateName(int length)
{
Random r = new Random();
string[] consonants = {"b","c","d","f","g","h","j","k","l","m","n","p","q","r","s","sh","z","zh",
"t","v","w","x","y"};
string[] vowels = { "a", "e", "i", "o", "u"};
string stringName; //Name as a string
string[] arrayName = new string[length]; //Name as array
bool Cons; //Whether the next letter added to the name array is a consonant or not.
int a = random(2); //Used to determine the value of the boolean Cons
if (a == 1) Cons = true; else Cons = false;
Cons = true;
int i = 1;
Cons = false;
while (i < length)
{
if(Cons)
{
arrayName[i] = consonants[random(consonants.Length)];
Cons = false;
i++;
}
else
{
arrayName[i] = vowels[random(vowels.Length)];
Cons = true;
i++;
}
}
arrayName[0] = arrayName[0].ToUpper();
stringName = string.Join("", arrayName);
return stringName;
}
Last edited by DerAxeEffekt (April 1, 2015 17:47:14)
(insert oversized quotation mark) If it is on the internet, it must be true. (insert another oversized quotation mark)
-Abraham Lincoln
- AonymousGuy
- Scratcher
1000+ posts
Random name generator in C#
You should put that in [code] tags so it doesn't interpret anything as BBCode:
public static string generateName(int length) { Random r = new Random(); string[] consonants = {"b","c","d","f","g","h","j","k","l","m","n","p","q","r","s","sh","z","zh", "t","v","w","x","y"}; string[] vowels = { "a", "e", "i", "o", "u"}; string stringName; string[] arrayName = new string[length]; bool Cons; int a = random(2); if (a == 1) Cons = true; else Cons = false; Cons = true; int i = 1; Cons = false; while (i < length) { if(Cons) { arrayName[i] = consonants[random(consonants.Length)]; Cons = false; i++; } else { arrayName[i] = vowels[random(vowels.Length)]; Cons = true; i++; } } arrayName[0] = arrayName[0].ToUpper(); stringName = string.Join("", arrayName); return stringName; }
- DerAxeEffekt
- Scratcher
100+ posts
Random name generator in C#
Oh. Thanks for telling me that!
(insert oversized quotation mark) If it is on the internet, it must be true. (insert another oversized quotation mark)
-Abraham Lincoln
- Superdoggy
- Scratcher
1000+ posts
Random name generator in C#
I added some comments - hopefully those help.
Sorry if it looks like I'm picking apart your code (just trying to help) but it looks like there's more than one problem going on.
public static string generateName(int length) { Random r = new Random(); //What's the point of this variable if you use "int a = random(2)" later anyway? string[] consonants = {"b","c","d","f","g","h","j","k","l","m","n","p","q","r","s","sh","z","zh", "t","v","w","x","y"}; string[] vowels = { "a", "e", "i", "o", "u"}; string stringName; string[] arrayName = new string[length]; bool Cons; int a = random(2); if (a == 1) Cons = true; else Cons = false; //Huh? Cons = true; //Wut? Cons = false; //By here "Cons" = false no matter anything that happened before...? int i = 1; //i should start at 0... while (i < length) //should be "length - 1" to fix issue with i { if(Cons) { arrayName[i] = consonants[random(consonants.Length)]; //because array indexes start at 0. Cons = false; i++; } else { arrayName[i] = vowels[random(vowels.Length)]; Cons = true; i++; } } arrayName[0] = arrayName[0].ToUpper(); stringName = string.Join("", arrayName); //Join arrayName? Why join? And you need an indexer, you can't just access the array like that! return stringName; //Woah, woah, woah, returning the stringName already? You need a foreach loop to individually //add each letter of arrayName to stringName! }
Sorry if it looks like I'm picking apart your code (just trying to help) but it looks like there's more than one problem going on.
Last edited by Superdoggy (April 1, 2015 18:17:28)
- DerAxeEffekt
- Scratcher
100+ posts
Random name generator in C#
You're good, I didn't notice any of those mistakes. By the way, about random(), this non-functional random name generator is a part of a larger project, and since I'm going to use random numbers alot, I made the random() method so I wouldn't have to type “r.Next()” all the time. I'm lazy. It seems that when I pasted the code from the larger project, I didn't notice that the method was still there.
About the random name generator, I found a better way to do it on the internet.
About the random name generator, I found a better way to do it on the internet.
public static string GenerateName(int len)
{
Random r = new Random();
string[] consonants = { "b", "c", "d", "f", "g", "h", "j", "k", "l", "m", "l", "n", "p", "q", "r", "s", "sh", "zh", "t", "v", "w", "x" };
string[] vowels = { "a", "e", "i", "o", "u", "ae", "y" };
string Name = "";
Name += consonants[r.Next(consonants.Length)].ToUpper();
Name += vowels[r.Next(vowels.Length)];
int b = 2; //b tells how many times a new letter has been added. It's 2 right now because the first two letters are already in the name.
while (b < len)
{
Name += consonants[r.Next(consonants.Length)];
b++;
Name += vowels[r.Next(vowels.Length)];
b++;
}
return Name;
}
(insert oversized quotation mark) If it is on the internet, it must be true. (insert another oversized quotation mark)
-Abraham Lincoln
- DerAxeEffekt
- Scratcher
100+ posts
Random name generator in C#
I forgot to add that there was a problem with that one too. When the following code is executed…
…it prints out a name, then a column of 9 names that are the same. Why is that?
for (int i = 0; i < 10; i++) Console.WriteLine(GenerateName(5));
…it prints out a name, then a column of 9 names that are the same. Why is that?
(insert oversized quotation mark) If it is on the internet, it must be true. (insert another oversized quotation mark)
-Abraham Lincoln
- Superdoggy
- Scratcher
1000+ posts
Random name generator in C#
The “Random” class generates numbers seeded on the current time via milliseconds. Every time you are generating a name you're making a new “Random” variable. Since C# executes commands amazingly fast (compared to scratch), your “Random” sequence of numbers is going to be the same every time. To fix this, put “Random r = new Random();” at the top of your class so it never goes out of scope. I forgot to add that there was a problem with that one too. When the following code is executed…for (int i = 0; i < 10; i++) Console.WriteLine(GenerateName(5));
…it prints out a name, then a column of 9 names that are the same. Why is that?
Last edited by Superdoggy (April 7, 2015 19:50:51)
- DerAxeEffekt
- Scratcher
100+ posts
Random name generator in C#
It worked! Thank you so much!
(insert oversized quotation mark) If it is on the internet, it must be true. (insert another oversized quotation mark)
-Abraham Lincoln
- ev3commander
- Scratcher
500+ posts
Random name generator in C#
I'm pretty sure you'll get names that are weird like “Yibdh” or something like that.
- ev3commander
- Scratcher
500+ posts
Random name generator in C#
Fine then “zhisoj”? Not really useful..I'm pretty sure you'll get names that are weird like “Yibdh” or something like that.
Actually that's impossible based on the code, it goes Consonant Vowel Consonant Vowel.. etc…
Last edited by ev3commander (April 8, 2015 16:24:04)
- Superdoggy
- Scratcher
1000+ posts
Random name generator in C#
You're totally missing the point. DerAxeEffekt is learning C#, not trying to rule the world with an epic name generator. the point is to learn the language by experimentation, so it doesn't have to be perfect.Fine then “zhisoj”? Not really useful..I'm pretty sure you'll get names that are weird like “Yibdh” or something like that.
Actually that's impossible based on the code, it goes Consonant Vowel Consonant Vowel.. etc…
- Discussion Forums
- » Advanced Topics
- » Random name generator in C#