Random Number Generation

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

    Random Number Generation

    I had someone write a random number generator in C# (I am more of a VB
    programmer) and they came up with the following:

    public string GetRand(int count)
    {
    string number = "";
    for (int i=0; i<count; i++)
    {
    Random Rnd = new Random();
    number = number+Convert. ToString(Rnd.Ne xt(0,9));
    }
    return number;
    }

    I want to return a string of digits where each each string is unique.
    Unfrotunately, this function is retrning strings like "11111111" and
    "44444444" and so on. This means the seed is not getting initialized on
    each interation but only on the first one. Now in VB, I would have put the
    Randomize() satement inside the loop so a new seed is innitialized. Is
    there an equivalent in C#? Is there a better way to do what we need?

    Thanx,
    --
    Anil Gupte




  • =?ISO-8859-1?Q?Arne_Vajh=F8j?=

    #2
    Re: Random Number Generation

    Anil Gupte wrote:
    I had someone write a random number generator in C# (I am more of a VB
    programmer) and they came up with the following:
    >
    public string GetRand(int count)
    {
    string number = "";
    for (int i=0; i<count; i++)
    {
    Random Rnd = new Random();
    number = number+Convert. ToString(Rnd.Ne xt(0,9));
    }
    return number;
    }
    >
    I want to return a string of digits where each each string is unique.
    Unfrotunately, this function is retrning strings like "11111111" and
    "44444444" and so on. This means the seed is not getting initialized on
    each interation but only on the first one. Now in VB, I would have put the
    Randomize() satement inside the loop so a new seed is innitialized. Is
    there an equivalent in C#? Is there a better way to do what we need?
    Try:

    private static Random Rnd = new Random();
    public string GetRand(int count)
    {
    string number = "";
    for (int i=0; i<count; i++)
    {
    number = number+Convert. ToString(Rnd.Ne xt(0,9));
    }
    return number;
    }

    Arne

    Comment

    • =?ISO-8859-1?Q?Arne_Vajh=F8j?=

      #3
      Re: Random Number Generation

      Anil Gupte wrote:
      I had someone write a random number generator in C# (I am more of a VB
      programmer) and they came up with the following:
      >
      public string GetRand(int count)
      {
      string number = "";
      for (int i=0; i<count; i++)
      {
      Random Rnd = new Random();
      number = number+Convert. ToString(Rnd.Ne xt(0,9));
      }
      return number;
      }
      >
      I want to return a string of digits where each each string is unique.
      Unfrotunately, this function is retrning strings like "11111111" and
      "44444444" and so on. This means the seed is not getting initialized on
      each interation but only on the first one. Now in VB, I would have put the
      Randomize() satement inside the loop so a new seed is innitialized. Is
      there an equivalent in C#? Is there a better way to do what we need?
      BTW, random strings are not unique !

      Arne

      Comment

      • Anil Gupte

        #4
        Re: Random Number Generation

        I thought Random strings should be unique. I understand that this is a
        pseudo random number being generated, but as I understand it, the seed is
        generated from the system clock and so there is at least a good chance that
        they are unique. In fact, I realize that even in a perfect random number
        generator there is a chance that the same number will keep appearing, but
        the probability of that is fairly low.

        Anyway, thanx for that function in your previous message, I will try it.

        --
        Anil Gupte



        "Arne Vajhøj" <arne@vajhoej.d kwrote in message
        news:471ad40e$0 $90265$14726298 @news.sunsite.d k...
        Anil Gupte wrote:
        >I had someone write a random number generator in C# (I am more of a VB
        >programmer) and they came up with the following:
        >>
        >public string GetRand(int count)
        >{
        >string number = "";
        >for (int i=0; i<count; i++)
        >{
        >Random Rnd = new Random();
        >number = number+Convert. ToString(Rnd.Ne xt(0,9));
        >}
        >return number;
        >}
        >>
        >I want to return a string of digits where each each string is unique.
        >Unfrotunatel y, this function is retrning strings like "11111111" and
        >"44444444" and so on. This means the seed is not getting initialized on
        >each interation but only on the first one. Now in VB, I would have put
        >the Randomize() satement inside the loop so a new seed is innitialized.
        >Is there an equivalent in C#? Is there a better way to do what we need?
        >
        BTW, random strings are not unique !
        >
        Arne

        Comment

        • =?ISO-8859-1?Q?G=F6ran_Andersson?=

          #5
          Re: Random Number Generation

          Anil Gupte wrote:
          I thought Random strings should be unique.
          No, the strings are not guaranteed to be unique. If they were, they
          would not be random. Each random number has the same probability for any
          number in the range, it won't have lower probability for numbers that
          has been used before.

          If you mean that each random values has an even probability in the
          range, that's true, but that is not uniqueness.
          I understand that this is a
          pseudo random number being generated, but as I understand it, the seed is
          generated from the system clock and so there is at least a good chance that
          they are unique. In fact, I realize that even in a perfect random number
          generator there is a chance that the same number will keep appearing, but
          the probability of that is fairly low.
          The Random object is only seeded from the clock when it's created. The
          subsequent random numbers are seeded from the previous random number.

          If you create a new Random object for every random number you pick, it
          will be seeded from the system clock every time. If you do that too
          close in time, like in the loop of your method, the system clock haven't
          changed, and you get the same random number over and over.

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

          Comment

          • rossum

            #6
            Re: Random Number Generation

            On Sun, 21 Oct 2007 09:38:27 +0530, "Anil Gupte"
            <anil-list@icinema.co mwrote:
            >I had someone write a random number generator in C# (I am more of a VB
            >programmer) and they came up with the following:
            >
            >public string GetRand(int count)
            >{
            >string number = "";
            Better to use StringBuilder and append.
            >for (int i=0; i<count; i++)
            >{
            >Random Rnd = new Random();
            See comments below.
            >number = number+Convert. ToString(Rnd.Ne xt(0,9));
            Rnd.Next(10) has the same effect.
            >}
            >return number;
            >}
            >
            >I want to return a string of digits where each each string is unique.
            The code you have does nothing to make sure that the string produced
            is unique. One way to avoid repeated strings is to use a different
            seed for each string, with sufficiently long strings there is a
            one-to-one relationship between strings and seeds.
            >Unfrotunatel y, this function is retrning strings like "11111111" and
            >"44444444" and so on.
            Using new Random() initialises the RNG from the clock. Putting this
            in a tight loop means that the clock does not have time to tick over
            between calls, so all the digits generated are repeats. THe code need
            to move the new Random() outside the loop.
            >This means the seed is not getting initialized on
            >each interation but only on the first one.
            It is initialised on each loop, but because the clock has not changed
            it is being initialised to the same value and giving the same output.
            >Now in VB, I would have put the
            >Randomize() satement inside the loop so a new seed is innitialized. Is
            >there an equivalent in C#? Is there a better way to do what we need?
            >
            >Thanx,
            Incorporating this into code, and using StringBuilder rather than
            creating a lot of immutable strings, I would write:

            public string GetRand(int seed, int count) {
            Random rand = new Random(seed);
            StringBuilder sb = new StringBuilder(c ount);
            for (int i = 0; i < count; ++i) {
            sb.Append((char )('0' + rand.Next(10))) ;
            }
            return sb.ToString();
            }

            For long enough strings, each seed will give a unique string. If you
            never repeat a seed then you will never repeat a string.

            rossum

            Comment

            • =?ISO-8859-1?Q?Arne_Vajh=F8j?=

              #7
              Re: Random Number Generation

              Anil Gupte wrote:
              I thought Random strings should be unique. I understand that this is a
              pseudo random number being generated, but as I understand it, the seed is
              generated from the system clock and so there is at least a good chance that
              they are unique. In fact, I realize that even in a perfect random number
              generator there is a chance that the same number will keep appearing, but
              the probability of that is fairly low.
              If you generate 2 numbers with 5 digits this way the probability
              is 1/10000 that there will be a duplicate.

              If you generate 100 numbers with 5 digits this way the probability
              is about 40% that there will be a duplicate.

              If you google for uuid or guid you will find better approaches
              to ensure uniqueness (even though some of them include random
              generators).

              Arne

              Comment

              • Anil Gupte

                #8
                Re: Random Number Generation

                Thanx!

                --
                Anil Gupte



                "rossum" <rossum48@coldm ail.comwrote in message
                news:799mh312ct kv1jiju6f0efjbg ugeeo7h7v@4ax.c om...
                On Sun, 21 Oct 2007 09:38:27 +0530, "Anil Gupte"
                <anil-list@icinema.co mwrote:
                >
                >>I had someone write a random number generator in C# (I am more of a VB
                >>programmer) and they came up with the following:
                >>
                >>public string GetRand(int count)
                >>{
                >>string number = "";
                Better to use StringBuilder and append.
                >
                >>for (int i=0; i<count; i++)
                >>{
                >>Random Rnd = new Random();
                See comments below.
                >
                >>number = number+Convert. ToString(Rnd.Ne xt(0,9));
                Rnd.Next(10) has the same effect.
                >>}
                >>return number;
                >>}
                >>
                >>I want to return a string of digits where each each string is unique.
                The code you have does nothing to make sure that the string produced
                is unique. One way to avoid repeated strings is to use a different
                seed for each string, with sufficiently long strings there is a
                one-to-one relationship between strings and seeds.
                >
                >>Unfrotunately , this function is retrning strings like "11111111" and
                >>"44444444" and so on.
                Using new Random() initialises the RNG from the clock. Putting this
                in a tight loop means that the clock does not have time to tick over
                between calls, so all the digits generated are repeats. THe code need
                to move the new Random() outside the loop.
                >
                >>This means the seed is not getting initialized on
                >>each interation but only on the first one.
                It is initialised on each loop, but because the clock has not changed
                it is being initialised to the same value and giving the same output.
                >
                >>Now in VB, I would have put the
                >>Randomize() satement inside the loop so a new seed is innitialized. Is
                >>there an equivalent in C#? Is there a better way to do what we need?
                >>
                >>Thanx,
                >
                Incorporating this into code, and using StringBuilder rather than
                creating a lot of immutable strings, I would write:
                >
                public string GetRand(int seed, int count) {
                Random rand = new Random(seed);
                StringBuilder sb = new StringBuilder(c ount);
                for (int i = 0; i < count; ++i) {
                sb.Append((char )('0' + rand.Next(10))) ;
                }
                return sb.ToString();
                }
                >
                For long enough strings, each seed will give a unique string. If you
                never repeat a seed then you will never repeat a string.
                >
                rossum
                >

                Comment

                • Anil Gupte

                  #9
                  Re: Random Number Generation

                  Thanx!

                  --
                  Anil Gupte



                  "Göran Andersson" <guffa@guffa.co mwrote in message
                  news:%23UFCE97E IHA.5328@TK2MSF TNGP05.phx.gbl. ..
                  Anil Gupte wrote:
                  >I thought Random strings should be unique.
                  >
                  No, the strings are not guaranteed to be unique. If they were, they would
                  not be random. Each random number has the same probability for any number
                  in the range, it won't have lower probability for numbers that has been
                  used before.
                  >
                  If you mean that each random values has an even probability in the range,
                  that's true, but that is not uniqueness.
                  >
                  >I understand that this is a pseudo random number being generated, but as
                  >I understand it, the seed is generated from the system clock and so there
                  >is at least a good chance that they are unique. In fact, I realize that
                  >even in a perfect random number generator there is a chance that the same
                  >number will keep appearing, but the probability of that is fairly low.
                  >
                  The Random object is only seeded from the clock when it's created. The
                  subsequent random numbers are seeded from the previous random number.
                  >
                  If you create a new Random object for every random number you pick, it
                  will be seeded from the system clock every time. If you do that too close
                  in time, like in the loop of your method, the system clock haven't
                  changed, and you get the same random number over and over.
                  >
                  --
                  Göran Andersson
                  _____
                  http://www.guffa.com

                  Comment

                  Working...