Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Next() in C# Random class not working when called from method

Posted on 2014-07-28
2
Medium Priority
?
545 Views
Last Modified: 2014-07-28
Hello,

This program is supposed to write random values for x and y in a web form's label named Label1. The web form's name is RandomXandY.aspx. Below is the Code Behind.

  public partial class RandomXandY : System.Web.UI.Page
  {
    protected void Page_Load(object sender, EventArgs e)
    {
      int x = 100;
      int y = 100;

      while (x > 0 && y > 0)
      {
        x = randomize(x);
        y = randomize(y);

        Label1.Text += "<br/><br/> x = " + x.ToString();
        Label1.Text += "<br/> y = " + y.ToString();
      }
    }

    private int randomize(int number)
    {
      Random random = new Random();  
      number -= random.Next(1, 10);
      return number;
    }

However, random.Next() keeps assigning the same values to x and y. So x and y are always equal. In order to truly randomize the values for x and y, I have to give the Random object global scope like this.

  public partial class RandomXandY : System.Web.UI.Page
  {
    Random random = new Random();

    protected void Page_Load(object sender, EventArgs e)
    {
      int x = 100;
      int y = 100;

      while (x > 0 && y > 0)
      {
        x = randomize(x);
        y = randomize(y);

        Label1.Text += "<br/><br/> x = " + x.ToString();
        Label1.Text += "<br/> y = " + y.ToString();
      }
    }

    private int randomize(int number)
    {        
      number -= random.Next(1, 10);
      return number;
    }
  }

How can I make random.Next() work properly from the randomize() method without declaring the Random object globally?

Thanks
Manny
0
Comment
Question by:MBarongan
2 Comments
 
LVL 21

Accepted Solution

by:
Randy Poole earned 2000 total points
ID: 40225959
It keeps giving the same number because it is called so quickly it is seeded via the system time.
 public partial class RandomXandY : System.Web.UI.Page
  {
    

    protected void Page_Load(object sender, EventArgs e)
    {
      int x = 100;
      int y = 100;

      while (x > 0 && y > 0)
      {
        x = randomize(x);
        y = randomize(y);

        Label1.Text += "<br/><br/> x = " + x.ToString();
        Label1.Text += "<br/> y = " + y.ToString();
      }
    }

    private int randomize(int number)
    {     
      //seed the number generator   
      Random random=new Random(int.Parse(Guid.NewGuid().ToString().Substring(0, 8), System.Globalization.NumberStyles.HexNumber));

      number -= random.Next(1, 10);
      return number;
    }
  }

Open in new window

0
 

Author Closing Comment

by:MBarongan
ID: 40225963
Thank you Randy. That worked. I'll review all the overloaded versions of Next() on MSDN to familiarize myself with this approach.
0

Featured Post

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a fe…
We’ve all felt that sense of false security before—locking down external access to a database or component and feeling like we’ve done all we need to do to secure company data. But that feeling is fleeting. Attacks these days can happen in many w…
Screencast - Getting to Know the Pipeline

772 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question