Automatic name generator

Asked

Viewed 1,390 times

-3

It’s the secret I needed when someone clicked a button in a text box appeared a name randomly for example

I have a list: Neuro, Food, Capitalism

when I clicked the button, the textbox appeared

Neuro Capitalism

or

food capitalism

or

Neuro Food

Update I’ve been doing some more research and I’ve managed to do this

private void button1_Click(object sender, EventArgs e)
{
    var words = new[] { "apple", "mango", "papaya", "banana", "guava", "pineapple" };
    var wordss = new[] { "apple", "mango", "papaya", "banana", "guava", "pineapple" };
    var wordsInRandomOrder = words.OrderBy(i => Guid.NewGuid());
    var wordsInRandomOrders = wordss.OrderBy(i => Guid.NewGuid());

    foreach (var word in wordsInRandomOrder)
    {
        textBox1.Text = word;
    }
    foreach (var word in wordsInRandomOrders)
    {
        textBox1.Text = textBox1.Text + wordss;
    }
} 

the problem is that in this part

foreach (var word in wordsInRandomOrders)
{
    textBox1.Text = textBox1.Text + wordss;
}

instead of putting one of the words in var wordss

He’s putting up with it

pineappleSystem.String[]System.String[]System.String[]System.String[]System.String[]System.String[]

What I have to do.

Update 2 I’ve been altering the code and now it’s like this

        var words = new[] { "apple", "mango", "papaya", "banana", "guava", "pineapple" };
        var wordsInRandomOrder = words.OrderBy(i => Guid.NewGuid());

    foreach (var word in wordsInRandomOrder)
    {
        textBox1.Text = word;
    }

    foreach (var word in wordsInRandomOrder)
    {
        textBox1.Text = textBox1.Text + word;
    }
}

It already appears pineappleSystem.String[]System.String[]System.String[]System.String[]System.String[]System.String[]

but now appears

guavamangobananapineappleappleguavapapaya

How do I change the formula to do only once.

  • 1

    And what is your question? This site is not a forum and much less a classified to find programmers. If you haven’t done it yet, do the [tour] and read [Ask].

  • @Luizvieira my doubt is like doing what I demonstrated on top I try but I can not find anywhere as it is done

  • 1

    "How to do" is very broad. It has numerous possibilities. Which is the one you tried? You say "tries but can’t". How about putting in the code snippet you’ve already tried and explaining where you believe you’re having trouble? It makes it easier for someone to help you.

  • @Luizvieira I did not find how to do what I want

  • 1

    Is that you will hardly find a tutorial or material that describes exactly what do you want, right? Have you studied the parts of your problem? Do you already know how to represent the names within your program? Do you already know how to draw a random number? Anyway, do you even understand the logic behind solving your problem? Don’t just look for the magic solution, you need to reflect a little on the problem. And don’t take this comment at all, okay? I’m really trying to help you. : ) Each of these questions can generate a question of yours here on the site, but you need to be objective(a).

  • @Luizvieira I always do a thing I see code I try to understand if I don’t ask

  • 1

    Great. You really have to ask. But that is precisely where the problem lies: your question, as it stands, is not clear because I cannot know exactly where your difficulty is (because it seems that your difficulty is: "I don’t know how to do anything" - and if that’s the case, maybe you should really hire someone to do it instead). Do you understand? And as I said earlier, this site is not a forum or a classified one. Really, do the [tour] and read [Ask]. It will be useful. :)

  • 1

    Tip: click [Edit] and ask the question the code snippet you already get and (maybe) print the names of the lists you use. Dai makes it easier to help you, proposing a solution of how to draw and join the names.

  • @Luizvieira I’m here to learn I do the following start making a program, if I can’t do something I google and youtube if I can’t come to this site and ask if I put code is because I know a little or I think you’re right in that part, if I don’t, it’s because I’m trying to learn and I can’t find any groove that can help me.

  • Improved with editing. I withdrew my vote to close. :)

  • 1

    @Luizvieira I will edit again already I changed the code again

Show 6 more comments

2 answers

3

Pekita, first point .OrderBy(i => Guid.NewGuid()) is an interrelative technique when we want to make a Randomize in the array, as in the extension below.:

public static IEnumerable<T> Shuffle<T>(this IEnumerable<T> items)
{
    return items.OrderBy(i => Guid.NewGuid());
}

But you don’t need to "shuffle" the whole Array to pick up an element randomly... imagine if your collection had 5,000 records.

Another point, there are already several blibiotecas to mount random strings, one of them is the Faker.Net, it is available for download on Nuget and the Source is available on GitHub.

So from now on, I advise you to start looking for packages by NuGet and study their respective source codes (most are available on NuGet or SourceForge)

Now when to get a random combination of strings.:

var random = new Random();
var nomes = new[] { "Aaliyah", "Aaron", "Abagail", "Abbey", "Abbie", "Abbigail", "Abby", "Abdiel" };
var sobrenomes = new[] { "Abbott", "Abernathy", "Abshire", "Adams", "Altenwerth", "Anderson", "Ankunding", "Armstrong", "Auer", "Aufderhar" };
return nomes[random.Next(nomes.length)] + " " + sobrenomes[random.Next(sobrenomes.length)];

0

I discovered the answer I remembered a code I used on the switch break; so I did the same thing on Forever. But if you know any simpler way to do the code I would appreciate it if you would tell me

private void button1_Click(object sender, EventArgs e)
{
    var words = new[] { "apple", "mango", "papaya", "banana", "guava", "pineapple" };
    var wordsInRandomOrder = words.OrderBy(i => Guid.NewGuid());

    foreach (var word in wordsInRandomOrder)
    {
        textBox1.Text = word;
        break;
    }

    foreach (var word in wordsInRandomOrder)
    {
        textBox1.Text = textBox1.Text + " " + word;
        break;
    }

Browser other questions tagged

You are not signed in. Login or sign up in order to post.