0

Thank you for helping me!

There is a piece of random function I found on the Internet, called GetRandom. I put it like below

 public static double GetRandom(double[] array)
  {    
       Random ran = new Random();
    int n = ran.Next(array.Length);
    return array[n];
  }

 private void Window_Activated(object sender, System.EventArgs e)
    {   


        c1 = c-GetRandom(arr);
        c2 = c-GetRandom(arr);
......

    }

arr is a double arry containing a a few doubles. c1,c2,c3 .... are all double. Howeve, it seems that every GetRandom(arr) in the last few lines return the same result.How come? The n in GetRandom can't be refreshed after each use?

Lu Huang
  • 25
  • 5

1 Answers1

0

You need move create Random outside function by create Random ran = new Random(); in class scope.

 Random ran = new Random();
 public static double GetRandom(double[] array, Random ran)
      {     
        int n = ran.Next(array.Length);
        return array[n];
      }

private void Window_Activated(object sender, System.EventArgs e)
    {   

        c1 = c-GetRandom(arr, ran);
        c2 = c-GetRandom(arr, ran);

    }
Hien Nguyen
  • 24,551
  • 7
  • 52
  • 62