jeudi 1 février 2018

C# Can I identify what Random selects between the range?

I want to be able to determine what the value is selected when I get a random number in windows forms. For example, int mynum = numbers[rand.Next(0,7)]; I know the range is between 0 and 7, but I want to be able to identify what number is selected. e.g. numbers[3]

The following is the code I have. I have read binary values into an array int [] numbers and passed it through the following method. It generates my random 3 bit binary value, but I am making a small quiz where the number identified is the value the random number selects (e.g. the location 3 is the correct value for the item stored there, numbers[i] ==answer).

Is there a way to access this value?

public void generateBinary3bit(int[] numbers, Form1 f1)
        {
            Random rand = new Random();
            int mynum = numbers[rand.Next(0,7)];

            **int ans = numbers[rand];** 

            f1.lblBinaryText.Text = mynum.ToString();
        }

For context, I am reading in the values into the array here:

int [] numbers = new int[8];

  public void readNumbers(int[]numbers, Form1 F1)
        {
            try
            {
                StreamReader sr = new StreamReader(@"C:\BinaryNumbers.csv");

                while(sr.ReadLine() !=null)
                {
                    for (int i = 0; i < numbers.Length; i++)
                    {
                        numbers[i] = Convert.ToInt32(sr.ReadLine());
                        answer = i;
                    }
                }
                sr.Close();
            }
            catch ( Exception e)
            {
            }
        }

Thank you,




Aucun commentaire:

Enregistrer un commentaire