Solved

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

Posted on 2014-07-28
2
516 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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 Reduce Deployment Times With Pre-Baked AMIs

Even if we can't include all the files in the base image, we can sometimes include some of the larger files that we would otherwise have to download, and we can also sometimes remove the most time-consuming steps. This can help a lot with reducing deployment times.

Question has a verified solution.

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

IntroductionWhile developing web applications, a single page might contain many regions and each region might contain many number of controls with the capability to perform  postback. Many times you might need to perform some action on an ASP.NET po…
Introduction This article shows how to use the open source plupload control to upload multiple images. The images are resized on the client side before uploading and the upload is done in chunks. Background I had to provide a way for user…
In this video, viewers will be given step by step instructions on adjusting mouse, pointer and cursor visibility in Microsoft Windows 10. The video seeks to educate those who are struggling with the new Windows 10 Graphical User Interface. Change Cu…
Monitoring a network: why having a policy is the best policy? Michael Kulchisky, MCSE, MCSA, MCP, VTSP, VSP, CCSP outlines the enormous benefits of having a policy-based approach when monitoring medium and large networks. Software utilized in this v…

632 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