1
Answer

Counting one problem - a BitString problem

Hell every one, I'm Carlos, new in this corner. architect and completely fun of programming.
I have a little problem to generate random numbers.

the thing that I want to resolve consist in to know the elements (1 or 0) in a bitstring of  250 elements. to do that I'm generating  a new bitstring and know what it is my probability of rightly (50%) aprox. then I generate a new bitstring (with the same function) and try to compare if my porcent its, better or not, and continue doing the same thing until find the complete bitstring.

But... when I generate the second bitstring return me the same porcent of the first one. I thought the problem it's related with the seed in the rundomnumber.

I leave my code.
best to all

namespace BitString_Problem
{
    class BitString
    {
        static void Main()
        {
            //simple array to store and compare the C.
            int[] arrC = new int[2];

            //define la primera tira.
            int[] arrO = new int[250];
            int[] arrT = new int[250];
            int[] arrT2 = new int[250];

            //return the bitstring to find!!!
            arrO = GenBitString();
            arrT = AdaptativeGen();

            //función para calcular la cantidad de aciertos
            int C = myfuncCorresponding(arrO, arrT);

            //generacion para comparar.
            arrT2 = AdaptativeGen();
            int C2 = myfuncCorresponding(arrO, arrT2);

            Console.WriteLine(C);
            Console.WriteLine(C2);
            Console.ReadLine();
        }

        //random bitString of 1 and 0.
        static int[] GenBitString()
        {
            int[] G = new int[250];
            Random r = new Random(DateTime.Now.Millisecond);

            for (int i = 0; i <= G.GetUpperBound(0); i++)
            {

                double rnd = r.NextDouble() * DateTime.Now.Millisecond;
                if (rnd < (DateTime.Now.Millisecond * 0.5))
                {
                    G[i] = 0;
                }
                else
                {
                    G[i] = 1;
                }
            }
            return G;
         }

        static int[] AdaptativeGen()
        {
            int[] A = new int[250];
            Random randNumber = new Random();

            for (int i = 0; i <= A.GetUpperBound(0); i++)
            {
                double Valrandom = randNumber.NextDouble();
                if (Valrandom < 0.5)
                {
                    A[i] = 0;
                }
                else
                {
                    A[i] = 1;
                }
            }
            return A;
        }

        static int myfuncCorresponding(int[] O, int[] T)
        {
        int Ncounter = 0;
            for (int i = 0; i <= O.GetUpperBound(0); i++)
            {
                if (O[i] == T[i])
                {
                    Ncounter = Ncounter + 1;
                }
            }
        return Ncounter;
        }

    }
}
 

Answers (1)