Rolling my .Net dice!

Sul forum è da poco stato chiesto un problema che riguarda la generazione di numeri casuali... scartabellando nel mio hard-disk ho ritrovato la mia classe per simulare il lancio di dadi. La classe era nata in vista di alcune utilità per giochi di ruolo... ma torna comoda anche per altro :p
public class Dice
 {
  static Random theRandomInstance;
  static Dice(){
   theRandomInstance = new Random(unchecked((int)DateTime.Now.Ticks));
  }
  int diceFaces;
  public Dice():this(6){}
  public Dice(int DiceFaces){
   diceFaces = DiceFaces;
  }
  public int Roll(){
   return Roll(diceFaces);
  }
  public static int Roll(int DiceFaces){
   return theRandomInstance.Next(0, DiceFaces) + 1;
  }
  public static int Roll(int DiceNumber, int DiceFaces){
   int Result = 0;
   for(int i = 0; i < DiceNumber; i++ ){
    Result += Roll(DiceFaces);
   }
   return Result;
  }
  public static int ChooseOne(int minValue, int maxValue){
   return theRandomInstance.Next(minValue, maxValue) + 1;
  }
 }

Penso che indicherò questo elemento del mio Blog come risposta al problema :p

posted @ martedì 14 ottobre 2003 18:26

Print
Comments have been closed on this topic.
«aprile»
domlunmarmergiovensab
31123456
78910111213
14151617181920
21222324252627
2829301234
567891011