Solved

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

Posted on 2014-07-28
2
482 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 500 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

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Problem Hi all,    While many today have fast Internet connection, there are many still who do not, or are connecting through devices with a slower connect, so light web pages and fast load times are still popular.    If your ASP.NET page …
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…
Sending a Secure fax is easy with eFax Corporate (http://www.enterprise.efax.com). First, Just open a new email message.  In the To field, type your recipient's fax number @efaxsend.com. You can even send a secure international fax — just include t…
Internet Business Fax to Email Made Easy - With eFax Corporate (http://www.enterprise.efax.com), you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, fr…

758 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

Need Help in Real-Time?

Connect with top rated Experts

23 Experts available now in Live!

Get 1:1 Help Now