Automatic name generator

-3

It's the second thing I need when someone clicks a button in a text box to display a name for the chutes, for example

I have a list: Neuro, Food, Capitalism

When I clicked the button, the textbox appeared

Neuro Capitalism

or

capitalism food

or

Neuro Food

Update I've been doing some research and I've been able 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 one of the words in var wordss

He's looking for it

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

What I have to do.

Update 2 I've been to change 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;
    }
}

Already appears and does not appear pineappleSystem.String [] System.String [] System.String [] System.String [] System.String [] System.String []

but now it appears

guavamangobananapineappleappleguavapaya

How do I change forever to do only once.

    
asked by anonymous 10.08.2016 / 14:42

2 answers

3

Pekita, first point .OrderBy(i => Guid.NewGuid()) is an interresting technique when we want to randomize 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 do not have to "shuffle" all Array so you can grab an element randomly ... imagine if your collection had 5,000 records.

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

So, starting from now, I advise you to start looking for packages by NuGet and study their respective source codes (most are available in 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)];
    
10.08.2016 / 16:23
0

I discovered the answer I remembered a code that I used in the switch break; so I did the same in forever. But if you guys know some simpler way to do the code, I'd 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;
    }
    
10.08.2016 / 15:57