Solved

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

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

3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Problem to error 4 43
Tool Box 2 34
Export import database 4 41
I need help converting a bitmap to an image in VB.Net 1 0
For those of you who don't follow the news, or just happen to live under rocks, Microsoft Research released a beta SDK (http://www.microsoft.com/en-us/download/details.aspx?id=27876) for the Xbox 360 Kinect. If you don't know what a Kinect is (http:…
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…
Here's a very brief overview of the methods PRTG Network Monitor (https://www.paessler.com/prtg) offers for monitoring bandwidth, to help you decide which methods you´d like to investigate in more detail.  The methods are covered in more detail in o…
This video demonstrates how to create an example email signature rule for a department in a company using CodeTwo Exchange Rules. The signature will be inserted beneath users' latest emails in conversations and will be displayed in users' Sent Items…

914 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

16 Experts available now in Live!

Get 1:1 Help Now