Solved

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

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

Webinar: Aligning, Automating, Winning

Join Dan Russo, Senior Manager of Operations Intelligence, for an in-depth discussion on how Dealertrack, leading provider of integrated digital solutions for the automotive industry, transformed their DevOps processes to increase collaboration and move with greater velocity.

Question has a verified solution.

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

ASP.Net to Oracle Connectivity Recently I had to develop an ASP.NET application connecting to an Oracle database.As I am doing it first time ,I had to solve several problems. This article will help to such developers  to develop an ASP.NET client…
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…
I've attached the XLSM Excel spreadsheet I used in the video and also text files containing the macros used below. https://filedb.experts-exchange.com/incoming/2017/03_w12/1151775/Permutations.txt https://filedb.experts-exchange.com/incoming/201…

751 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