dimanche 6 mai 2018

Making a random number class of functions for C#

So I have been trying to make a class of functions to generate pseudo-random numbers in C#. The idea was to not have to constantly deal with generating the Random object all the time and use it just like one would use the random functions in for example Python by just calling them (My attempted code is attached below in block (1)). However, When it comes to Generating a random matrix using this function for a neural network genetic algorithm (Matrix function is attached in block (2)) I get some weight matrices that are the same.

I cannot seem to understand why this is happening because I took the precaution of not declaring my Random object in places where it could run the risk of having the seed reset. Is there something I'm missing here?

1)

using System.Collections;
using System.Collections.Generic;
using System;

namespace Neural
{

    public static class RNG
    {
        static Random random = new Random();
        //This is out here so the seed would not be constantly reset

        public static float GenerateFromUniform (float hi, float lo){

            float output = (float)((hi-lo)*random.NextDouble ()+lo);

            return output;
        }
}

2)

using System.Diagnostics;
using System;
using System.Collections;
using System.Collections.Generic;

namespace Neural
{
    public static class Matrix
    {
        public static float[,] UniformRandomMatrix(int n, int m, float hi, float lo)
        {
            float[,] R = new float [n,m];
            for (int i=0;i<n;i++){
                for (int j=0;j<m;j++){
                    R[i, j] = (float)(RNG.GenerateFromUniform(hi,lo));

                }
            }
        }
}




Aucun commentaire:

Enregistrer un commentaire