samedi 30 juillet 2016

cycle never reaching final value

This is my random unique numbers generator I try to create for my cards software. It generates numbers and write into array OK. I have problem with the loop here. when integer i reaches 29, it stops growing and code cycles infinitely and never reaches 30, which would stop the loop.

Without the if statement it works, but it won't fill the range needed.

public partial class Form1 : Form
{
    int[] rndCards = new int[30];
    public Form1()
    {
        InitializeComponent();
        richTextBox1.Text = "random numbers";


    }


    private void button1_Click(object sender, EventArgs e)
    {
        int i = 0;
        rndCards = new int[30];
        richTextBox1.Clear();
        Random rnd = new Random();
        while (i < 30)
        {
            int cardTest = rnd.Next(0, 30);
            while (rndCards.Contains(cardTest)) 
            {
                    cardTest++;
                    if (cardTest == 30)
                    {
                        cardTest = 0;
                    }
            }

            rndCards[i] = cardTest;
            richTextBox1.Text += rndCards[i] + ", ";
            i++;
        }
    }




Aucun commentaire:

Enregistrer un commentaire