Always the same random numbers

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Aspidus

    Always the same random numbers

    Hi,
    I wrote a function that generates a ulong[]

    I use it 3 times, one after the other, but it generates always the same
    numbers.

    At the end i get always 3 equal arrays.


    Any idea?

    Aspidus
  • =?ISO-8859-15?Q?G=F6ran_Andersson?=

    #2
    Re: Always the same random numbers

    Aspidus wrote:
    Hi,
    I wrote a function that generates a ulong[]
    >
    I use it 3 times, one after the other, but it generates always the same
    numbers.
    >
    At the end i get always 3 equal arrays.
    >
    >
    Any idea?
    >
    Aspidus
    A few ideas, but it would be far better if you showed your code so that
    I could give you a definitive answer instead of vague theories about it.

    --
    Göran Andersson
    _____
    Göran Anderssons privata hemsida.

    Comment

    • Barry Kelly

      #3
      Re: Always the same random numbers

      Aspidus wrote:
      I wrote a function that generates a ulong[]
      >
      I use it 3 times, one after the other, but it generates always the same
      numbers.
      >
      At the end i get always 3 equal arrays.
      If you use Random class but keep newly constructing it with 'new
      Random()' in a tight loop, you'll get the same values for a while. This
      is because the initial state of the Random class comes from the system
      clock, but a tight loop doesn't let the clock's value change.

      Create a single Random class instance in a field, and use it, rather
      than continuously creating new ones.

      -- Barry

      --

      Comment

      • Aspidus

        #4
        Re: Always the same random numbers

        Sorry you're right, here it is:


        idA = RndGen(LidA, W);
        idB = RndGen(LidB, W);
        Sh = RndGen(LSh, W);


        private ulong[] RndGen(int NumCifre, int Base)
        {
        ulong[] Risultato = new ulong[NumCifre];
        Thread.Sleep(15 ); //The generated arrays only differ if i
        put this instruction, but in this way i loose time to generate more
        arrays in few time (for statistic use)
        Random rnd = new Random(unchecke d((int)DateTime .Now.Ticks));

        for (int k = 0; k < NumCifre; k++)
        {

        if (k == 0 && Risultato[k] == 0)
        Risultato[k] = (ulong)rnd.Next (1, (Base - 1)); /
        else
        Risultato[k] = (ulong)rnd.Next (Base - 1);
        }

        return Risultato;
        }

        Thank you. Aspidus.


        Göran Andersson ha scritto:
        Aspidus wrote:
        >Hi,
        >I wrote a function that generates a ulong[]
        >>
        >I use it 3 times, one after the other, but it generates always the
        >same numbers.
        >>
        >At the end i get always 3 equal arrays.
        >>
        >>
        >Any idea?
        >>
        >Aspidus
        >
        A few ideas, but it would be far better if you showed your code so that
        I could give you a definitive answer instead of vague theories about it.
        >

        Comment

        • Aspidus

          #5
          Re: Always the same random numbers

          Sorry you're right, here it is:


          idA = RndGen(LidA, W);
          idB = RndGen(LidB, W);
          Sh = RndGen(LSh, W);


          private ulong[] RndGen(int NumCifre, int Base)
          {
          ulong[] Risultato = new ulong[NumCifre];
          Thread.Sleep(15 ); //The generated arrays only differ if i
          put this instruction, because the clock change its value, but in this
          way i loose time to generate more arrays in few time (for statistic use)
          Random rnd = new Random(unchecke d((int)DateTime .Now.Ticks));

          for (int k = 0; k < NumCifre; k++)
          {

          if (k == 0 && Risultato[k] == 0)
          Risultato[k] = (ulong)rnd.Next (1, (Base - 1)); /
          else
          Risultato[k] = (ulong)rnd.Next (Base - 1);
          }

          return Risultato;
          }

          Thank you. Aspidus.

          Göran Andersson ha scritto:
          Aspidus wrote:
          >Hi,
          >I wrote a function that generates a ulong[]
          >>
          >I use it 3 times, one after the other, but it generates always the
          >same numbers.
          >>
          >At the end i get always 3 equal arrays.
          >>
          >>
          >Any idea?
          >>
          >Aspidus
          >
          A few ideas, but it would be far better if you showed your code so that
          I could give you a definitive answer instead of vague theories about it.
          >

          Comment

          • Barry Kelly

            #6
            Re: Always the same random numbers

            Aspidus wrote:
            private ulong[] RndGen(int NumCifre, int Base)
            {
            Random rnd = new Random(unchecke d((int)DateTime .Now.Ticks));
            Don't do this. Create a Random instance in a field, and use the field.

            Also, just using 'new Random()' will essentially do what you are doing
            here, initializing it from the system clock. You don't need to pass the
            ticks manually.
            }
            -- Barry

            --

            Comment

            • Aspidus

              #7
              Re: Always the same random numbers

              I did it ;-) thank you! It works!

              Random rnd = new Random();
              idA = RndGen(LidA, W, rnd);
              idB = RndGen(LidB, W, rnd);
              Sh = RndGen(LSh, W, rnd);




              private ulong[] RndGen(int NumCifre, int Base, Random rnd)
              {
              ulong[] Risultato = new ulong[NumCifre];
              for (int k = 0; k < NumCifre; k++)
              {
              if (k == 0 && Risultato[k] == 0)
              Risultato[k] = (ulong)rnd.Next (1, (Base - 1));
              else
              Risultato[k] = (ulong)rnd.Next (Base - 1);
              }

              return Risultato;
              }



              Bye Aspidus.




              Barry Kelly ha scritto:
              Aspidus wrote:
              >
              >private ulong[] RndGen(int NumCifre, int Base)
              > {
              >
              >
              > Random rnd = new Random(unchecke d((int)DateTime .Now.Ticks));
              >
              Don't do this. Create a Random instance in a field, and use the field.
              >
              Also, just using 'new Random()' will essentially do what you are doing
              here, initializing it from the system clock. You don't need to pass the
              ticks manually.
              >
              > }
              >
              -- Barry
              >

              Comment

              • JR

                #8
                Re: Always the same random numbers



                JR


                "Aspidus" <aspidus@interf ree.it???
                ??????:45eb2f47 $0$37195$4fafba ef@reader3.news .tin.it...
                Hi,
                I wrote a function that generates a ulong[]
                >
                I use it 3 times, one after the other, but it generates always the same
                numbers.
                >
                At the end i get always 3 equal arrays.
                >
                >
                Any idea?
                >
                Aspidus

                Comment

                • Jon Skeet [C# MVP]

                  #9
                  Re: Always the same random numbers

                  Aspidus <aspidus@interf ree.itwrote:
                  I wrote a function that generates a ulong[]
                  >
                  I use it 3 times, one after the other, but it generates always the same
                  numbers.
                  >
                  At the end i get always 3 equal arrays.
                  >
                  Any idea?
                  See http://pobox.com/~skeet/csharp/miscu...ticrandom.html

                  --
                  Jon Skeet - <skeet@pobox.co m>
                  http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
                  If replying to the group, please do not mail me too

                  Comment

                  Working...