Random Number Generator in C#...

sumo_guy
sumo_guy used Ask the Experts™
on
I need help making a random number generaton in C#.  Can someone help me, I am actually looking to make a dice roller with it.
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®

Author

Commented:
these are all the points I can afford
Take a look at Random class. This should provide you way to generate random numbers.
Commented:
Here's a little example that rolls a 6-sided die 100 times and prints the result of each roll to the screen:


using System;

namespace ConsoleApplication1
{
     class Class1
     {
          public class DiceRoller
          {
               private System.Random rand=new System.Random();
               public DiceRoller(){}
               public int Roll(int min, int max)
               {
                    return rand.Next(min, max+1);
               }
          }

          static void Main(string[] args)
          {
               DiceRoller roller=new DiceRoller();
               for(int i=0;i<100;++i)
               {
                    //use a six sided die
                    int x=roller.Roll(1,6);
                    Console.WriteLine(x.ToString());
               }
          }
     }
}

Commented:
If performance is critical, consider the Mersenne Twister.  There's a free C# implementation at my site:

http://www.centerspace.net/resources.php

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial