dimanche 31 octobre 2021

Randomize index without repeating C#

I want to create a program which would show the flag in a picture box and a user would have to guess what flag it is. I have a problem that flags are repeating and I totally don't know how to fix it.

Here is a bit of my code:

private void button1_Click(object sender, EventArgs e)
{
    Random random = new Random();
    randomized = random.Next(0, 9);
//0 = Poland
//1 = France
//2 = Sweden
//3 = Germany
//4 = Portugal
//5 = Spain
//6 = Finland
//7 = Norway
//8 = Russia
//9 = Ukraine

    
    if (randomized == 0)
    {
        ptbFlag.Image = Image.FromFile("C:/Users/Kacper/Desktop/FlagiDoGeo/Poland.png");
    }

    if (randomized == 1)
    {
        ptbFlag.Image = Image.FromFile("C:/Users/Kacper/Desktop/FlagiDoGeo/France.png");
    }

    if (randomized == 2)
    {
        ptbFlag.Image = Image.FromFile("C:/Users/Kacper/Desktop/FlagiDoGeo/Sweden.jpg");
    }

    if (randomized == 3)
    {
        ptbFlag.Image = Image.FromFile("C:/Users/Kacper/Desktop/FlagiDoGeo/Germany.png");
    }

    if (randomized == 4)
    {
        ptbFlag.Image = Image.FromFile("C:/Users/Kacper/Desktop/FlagiDoGeo/Portugal.png");
    }

    if (randomized == 5)
    {
        ptbFlag.Image = Image.FromFile("C:/Users/Kacper/Desktop/FlagiDoGeo/Spain.png");
    }

    if (randomized == 6)
    {
        ptbFlag.Image = Image.FromFile("C:/Users/Kacper/Desktop/FlagiDoGeo/Finland.png");
    }

    if (randomized == 7)
    {
        ptbFlag.Image = Image.FromFile("C:/Users/Kacper/Desktop/FlagiDoGeo/Norway.png");
    }

    if (randomized == 8)
    {
        ptbFlag.Image = Image.FromFile("C:/Users/Kacper/Desktop/FlagiDoGeo/Russia.png");
    }

    if (randomized == 9)
    {
        ptbFlag.Image = Image.FromFile("C:/Users/Kacper/Desktop/FlagiDoGeo/Ukraine.png");
    }
}



Aucun commentaire:

Enregistrer un commentaire