mercredi 4 janvier 2017

Randomly generating an int value in for loop. After 7 iterations, it returns a very large negative number. Why is this?

The code below is rotating a wheel either 10 times or 1 time depending on whether or not the user selects 'yes'.

The rotation method takes a number between 0-100, and rotates the filter an amount based on that number. To test the machine, I want to generate a random number between 0-99. I feel this method should be able to do that. However, after 7 times, the random number produced is -2.147483465E+09 until the end of the run. The output, for example:

 89
 20
 1
 58
 78
 39
 90
 -2.147483465E+09
 -2.147483465E+09
 -2.147483465E+09

Could this be something related to garbage collection? The 'sUVFilter' is not changed to 0 by the method, these are the values it gets when I set it = to rand.Next();

Any help or information would be appreciated! Thanks a lot. Here is the code in question.

    private void RotateFilterWheel()
    {
        List<float> filterValuesList = new List<float>();
        int timesToRotate = 1;
        int success = 0;
        Random rand = new Random();

        MessageBoxResult result = MessageBox.Show("Rotate filter 10 times?", "Rotate Filter", MessageBoxButton.YesNo);
        if (result == MessageBoxResult.Yes)
        {
            timesToRotate = 10;
        }

        for (int x = 0; x < timesToRotate; x++)
        {

            int sUVFilter = rand.Next(99);
            _instDrvr.SetSpectroAutoUVFilterPosition(sUVFilter, ref success);
            filterValuesList.Add(sUVFilter);
            Thread.Sleep(5000);
        }
        listBox.ItemsSource = filterValuesList;
    }




Aucun commentaire:

Enregistrer un commentaire