Search code examples
c#randomperceptron

Random Number with NextDouble (C#)


I'm programming a perceptron and really need to get the range from the normal NextDouble (0, 1) to (-0.5, 0.5). Problem is, I'm using an array and I'm not sure whether it is possible. Hopefully that's enough information.

Random rdm = new Random();

double[] weights = {rdm.NextDouble(), rdm.NextDouble(), rdm.NextDouble()};

Solution

  • Simply subtract 0.5 from your random number:

    double[] weights = {
                         rdm.NextDouble() - 0.5, 
                         rdm.NextDouble() - 0.5, 
                         rdm.NextDouble() - 0.5 
                       };