About me

My name is Marko Markovic and I am a software developer, a musician and a researcher. I am intrigued by algorithms and new techniques and I want to improve my software development skills.

While working on different projects I like to document what I have learned. A new technique, a new algorithm, solution to a problem or a tool. The reason I am writing this blog is to help out other people in their programming endeavors and also I write so I won't forget.

If you are interested to find out more about me visit markomarks.com

Monday, November 28, 2011

Randomize

Today I'm inspired to share with you my C# class for random number generation. I've expanded an example from some site using my google fu but sorry I've forgotten the link.

I'm still using this class heavily in my master thesis and Video game development projects. I'm adding things in it as I need them.


First of all it's a static class. Simply said, it doesn't need initialization in the program you're using. Save it as separate .cs file so you could use it in the next program/project whatever.

How to use it? Simple. Just call RandomGenerator.functionName() and that's it.

private static Random randomSeed = new Random();

This is the main part. We have declared a Random variable. You could expand/tweek this by playing around with seed numbers, but for me, this does the job very nicely.

Functions. There are couple of functions for now. RandomBool gives us a random boolean value. RandomSignConstant gives us -1 or 1.  RandomNumberBounds gives us an int number between min and max value. RandomNumberCenter is a bit complicated. Simply put it creates a random number in the range of [0,max] but with a twist. You pass an argument of the center (didn't know how to name it properly because English is not my native language, but it means any point in the range), and also you pass an argument jump which defines the subrange. This function returns a random number between the numbers [center-jump, center+jump]. And finally RandomNumber returns a number between [0, maximal number].

The full class can be copied from here.
public static class RandomGenerator
  {
    private static Random randomSeed = new Random();
    public static bool RandomBool()
    {
      return (randomSeed.NextDouble() > 0.5);
    }
    public static int RandomSignConstant()
    {
      if (randomSeed.NextDouble() > 0.5)
      {
        return 1;
      }
      else
      {
        return -1;
      }
    }
    public static int RandomNumberBounds(int Minimal, int Maximal)
    {
      return randomSeed.Next(Minimal, Maximal);
    }
    public static int RandomNumberCenter(int Center, int Maximal, int Jump)
    {
      int minimalBound = 0;
      int maximalBound = 0;
      int tempCenter = 0;
      if (Center < 0)
      {
        tempCenter = 0;
      }
      else
      {
        tempCenter = Center;
      }
      if (tempCenter - Jump < 0)
      {
        minimalBound = 0;
      }
      else
      {
        minimalBound = tempCenter - Jump;
      }
      if (tempCenter + Jump > Maximal)
      {
        maximalBound = Maximal;
      }
      else
      {
        maximalBound = tempCenter + Jump;
      }
      return randomSeed.Next(minimalBound, maximalBound);
    }
    public static int RandomNumber(int Maximal)
    {
      return randomSeed.Next(Maximal);
    }
  }
That's it from me today, hope you've understood my Engrish and hope you'll find this function useful.

No comments:

Post a Comment

Blog Archive