简体   繁体   中英

Why is my array changing back and forth each time i press the button?

C# Windows Form Application - Visual Studio

This is my code that creates an array and sorts it by numerical value, step by step in a textbox. The only problem is that when i click the button multiple times it switches between the actual array and what seems to be another random array. Why is this?

    int[] randArray = new int[5];
    private void txtSeed_TextChanged(object sender, EventArgs e) {
        int notaninteger;
        if (!int.TryParse(txtSeed.Text, out notaninteger)) {
            txtSeed.Text = "0";
        }
        if (int.Parse(txtSeed.Text) < 0) {
            txtSeed.Text = "0";
        }
    }
    private void btnLotto_Click(object sender, EventArgs e) {
        fillArray();
        sortArray();
        showResult();
    }
    private void fillArray() {
        bool inArray = true;
        Random random = new Random(int.Parse(txtSeed.Text));
        for (int i = 0; i < randArray.Length; i++) {
            do {
                int randomNumber = random.Next(1, 51);
                inArray = checkArray(randomNumber);
                if (!inArray) {
                    randArray[i] = randomNumber;
                }
            }
            while (inArray);
        }
    }
    private void displayArray() {
        txtLotto.AppendText(getFormat());
        txtLotto.AppendText("\r\n");
    }
    private void sortArray() {
        txtLotto.Text = "";
        for (int i = 0; i < randArray.Length; i++) {
            for (int j = 0; j < randArray.Length - 1; j++) {
                if (randArray[j] > randArray[j + 1]) {
                    swap(ref randArray[j], ref randArray[j + 1]);
                    displayArray();
                }
            }
        }
    }
    private bool checkArray(int a) {
        for (int i = 0; i < randArray.Length; i ++) {
            if (randArray[i] == a) {
                return true;
            }
        }
        return false;
    }
    private string getFormat() {
        string format = "";
        for (int i = 0; i < randArray.Length; i ++) {
            if (i != 0) {
                format += "-";
            }
            format += randArray[i];
        }
        return format;
    }
    private void showResult() {
        lblFinalResult.Text = "Final Result:    ";
        string format = "";
        for (int i = 0; i < randArray.Length; i++) {
            if (i != 0) {
                format += "-";
            }
            format += randArray[i];
        }
        lblFinalResult.Text += format;
    }
    private void swap(ref int a, ref int b) {
        int tempA = a;
        a = b;
        b = tempA;
    }

https://msdn.microsoft.com/en-us/library/system.random(v=vs.110).aspx

If the same seed is used for separate Random objects, they will generate the same series of random numbers.

The reason that you have 2 different kind of arrays is because:

  1. Note that Random will always generate, for ex: 1,2,3,4,5,6,7,8,9,10 after initializing with same seed.
  2. you fill the array with the first 5 series, for ex: 1,2,3,4,5.
  3. the next time it tries to fill with the same amount 1,2,3,4,5, but your code cannot do that, because you avoid inputting duplicates, therefore you get and fill the array with the next 5 series, for ex: 6,7,8,9,10
  4. the next time you try to fill the array, it tries to fill with the same amount 1,2,3,4,5 again, you successfully input to the array.
  5. repeat 3~4

To solve your problem, basically the rule is just create a single Random instance and avoid reinitializing (with same seed). Therefore, you will always get different arrays every click.

private Random random = new Random(); // one instance of random
private int previousSeed = 0; // memorize previous seed
private void fillArray()
{
    bool inArray = true;
    int newSeed = int.Parse(txtSeed.Text);
    if (newSeed != previousSeed)
    {
        // reinitialize the random only if new seed is different
        random = new Random(newSeed);
        previousSeed = newSeed;
    }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM