Page view counter

Silverlight Tips of the Day - Blog by Mike Snow

Game Programming with Silverlight

Silverlight Tip of the Day #77 - Creating an Efficient Random Generator

In some of my older posts I was creating a separate random generator for each object. However, the documentation on Random states:

"The default seed value is derived from the system clock and has finite resolution. As a result, different Random objects that are created in close succession by a call to the default constructor will have identical default seed values and, therefore, will produce identical sets of random numbers. This problem can be avoided by using a single Random object to generate all random numbers."

As a result I created a single random generator class and I put it in a new static class called Utils where it could be globally referenced.

public class Utils
{
    static Random _random;
 
    static Utils()
    {
        _random = new Random();
    }
 
    public static int RndGen(int min, int max)
    {
        return _random.Next(min, max);
    }
}

To make a call now from one of your classes such as your Page class you simply make a call like this to get a random number:

int value = Utils.RndGen(1, 50);  

The first parameter is the minimum value you want, the second is the maximum.

I updated Tip of the Day #43 - Snowflake scene - to use this implementation.

Thank you,
--Mike Snow

 Subscribe in a reader

Comments

Microsoft Weblogs said:

In some of my older posts I was creating a separate random generator for each object. However, the documentation

# December 1, 2008 3:58 PM

jackbond said:

You could avoid tying your code to a particular application class (and thereby the unneccessary casting) by simply using a static method on a utility class to achieve the same result (sample below) One other issue, the silverlight docs make no mention of whether Next() is thread safe or not. If not, it would be pretty easy to lock on the _Random object (another reason to put this functionality in a separate class)

public class GlobalRandom

{

   static Random _Random;

   static GlobalRandom()

   {

       _Random = new Random();

   }

   public static int Next(int min, int max)

   {

       return _Random.Next(min, max);

   }

}

# December 1, 2008 4:52 PM

Odegaard said:

I couldn't agree more with JackBond. The pattern used above is horrible.

Just to add to the above example. I would use lazy instantiation, as well as making sure it's thread safe (see en.csharp-online.net/Singleton_design_pattern:_Thread-safe_Singleton)

# December 1, 2008 5:08 PM

mike.snow said:

Good points! I will update the post to use a static method as you showed.

# December 1, 2008 5:11 PM

GearWorld said:

Waaaaa I don't get it.  I look at both code and I don't see any difference else then the name of the method and the name of the class

Can you guys shed some light on it please ?

# December 1, 2008 5:32 PM

mike.snow said:

The main difference is that the class is static.

# December 1, 2008 5:42 PM

miguelito928 said:

Mike,

I expanded on your blog post with a few thoughts of my own on this subject.  See my blog post as a tribute to yours:

murrayon.net/.../correctly-using-random-number.html

# December 1, 2008 7:16 PM

jackbond said:

The differences are minor, but one of the primary ones is maintainability. First off, this functionality has nothing to do with the Application class itself, so why put it there and then require a cast? Or in the future, if he needed to refactor this code, and decided to put it in a library, he would need to update all the existing references. Better just to separate it out in the beginning (especially if it's going to encapsulate thread safetey as well.)

Somewhat related, and not nearly emphasized enough:

en.wikipedia.org/.../Law_of_Demeter

# December 1, 2008 7:56 PM

Community Blogs said:

In this issue: Alex Golesh, Silverlight SDK, John Stockton, Page Brooks(2), Mike Snow(2), Terence Tsang

# December 2, 2008 1:20 AM

2008 December 02 - Links for today « My (almost) Daily Links said:

Pingback from  2008 December 02 - Links for today « My (almost) Daily Links

# December 2, 2008 5:12 AM

Dew Drop - December 2, 2008 | Alvin Ashcraft's Morning Dew said:

Pingback from  Dew Drop - December 2, 2008 | Alvin Ashcraft's Morning Dew

# December 2, 2008 8:45 AM

Silverlight Tips of the Day - Blog by Mike Snow said:

The purpose of this post is to create an outline summary all the blogs from my Silverlight tips of the

# January 2, 2009 5:57 PM

Silverlight Tips of the Day - Blog by Mike Snow said:

The purpose of this post is to create an outline summary all the blogs from my Silverlight tips of the

# January 2, 2009 5:57 PM

o UAU nosso de cada dia said:

essa lista eu copiei desse blog bárbaro (acompanhe por RSS você também): uma lista de dicas super úteis

# January 3, 2009 6:26 AM

Visual Web Developer Team Blog said:

This link provides a complete Tips of the Day Summary Outline - silverlight.net/.../silverlight-tips-of-the-day-summary-outline.aspx

# January 8, 2009 6:36 PM