Same Random result ?!

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • mironline
    New Member
    • May 2008
    • 24

    Same Random result ?!

    dear friends .


    I want to create a instance of [R] class in [foreach] statement.
    but all 10 responses are same same!



    //
    Code:
    using System;
    
    public partial class Class1: System.Web.UI.Page
    {
    protected void Page_Load(object sender, EventArgs e)
    {
    string[] k = new string[10];
    R cl;
    foreach(string kx in k)
    {
    cl = new R();
    Response.Write(cl.Ra() + "<br>");
    }
    }
    }
    
    public class R
    {
    private Random r;
    public R()
    {
    r = new Random();
    }
    public int Ra()
    {
    return r.Next(100);
    }
    }
    Last edited by Plater; May 20 '08, 07:22 PM. Reason: code tags
  • Plater
    Recognized Expert Expert
    • Apr 2007
    • 7872

    #2
    Its because you do it so fast.
    The random seed comes from the timer object, all of those calls happen before it changes.

    Code:
    foreach (string kx in k)
    {
       cl = new R();
       Console.WriteLine(cl.Ra());//i was in a windows app so I changed it to this
       System.Threading.Thread.Sleep(100);
    }
    That gave me different numbers each time.

    I would recomend you maybe re-think your class design since Thread.Sleep() is probably not good in a webpage context.

    Perhaps like this:
    Code:
    public class R
    {
       private static Random r=new Random();
       public R()
       {
          //r = new Random();
       }
       public int Ra()
       {
          return r.Next(100);
       }
    }

    Comment

    • Curtis Rutland
      Recognized Expert Specialist
      • Apr 2008
      • 3264

      #3
      Unless there is more that you will be doing with this class, why do you need the class in the first place?

      The same results can be achieved much simpler:
      Code:
      public partial class _Default : System.Web.UI.Page
      {
          protected void Page_Load(object sender, EventArgs e)
          {
              string[] k = new string[10];
              Random cl = new Random();
              foreach (string kx in k)
                  Response.Write(cl.Next(100) + "<br>");
          }
      }
      Originally posted by Plater
      Its because you do it so fast.
      The random seed comes from the timer object, all of those calls happen before it changes.

      Code:
      foreach (string kx in k)
      {
         cl = new R();
         Console.WriteLine(cl.Ra());//i was in a windows app so I changed it to this
         System.Threading.Thread.Sleep(100);
      }
      That gave me different numbers each time.

      I would recomend you maybe re-think your class design since Thread.Sleep() is probably not good in a webpage context.

      Perhaps like this:
      Code:
      public class R
      {
         private static Random r=new Random();
         public R()
         {
            //r = new Random();
         }
         public int Ra()
         {
            return r.Next(100);
         }
      }

      Comment

      • mironline
        New Member
        • May 2008
        • 24

        #4
        I would recomend you maybe re-think your class design since Thread.Sleep() is probably not good in a webpage context.

        thank you very very much.

        Comment

        Working...