Cerebrus

Randomization

Jun 22nd, 2009
80
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.71 KB | None | 0 0
  1. //Related to Question at:
  2. //http://groups.google.com/group/dotnetdevelopment/browse_thread/thread/7e6a6815d8d855f9
  3.  
  4. using System;
  5. using System.Threading;
  6.  
  7. interface IFoo
  8. {
  9.   void foo(int a, int b);
  10. }
  11.  
  12. class Factory : IFoo
  13. {
  14.   private enum Operation
  15.   {
  16.     add = 1,
  17.     subtract = 2,
  18.     multiply = 3,
  19.     divide = 4,
  20.   }
  21.  
  22.   private int _a, _b;
  23.   private int randomSeed = 0;
  24.  
  25.   public virtual void foo(int a, int b)
  26.   {
  27.    
  28.     this._a = a;
  29.     this._b = b;
  30.  
  31.     Random rand = new Random(randomSeed);
  32.  
  33.     Operation op = (Operation)rand.Next(1, 5);
  34.  
  35.     switch (op)
  36.     {
  37.       case Operation.add:
  38.         plus();
  39.         break;
  40.       case Operation.subtract:
  41.         minus();
  42.         break;
  43.       case Operation.multiply:
  44.         multiplier();
  45.         break;
  46.       case Operation.divide:
  47.         dividor();
  48.         break;
  49.     }
  50.  
  51.     //if (rand.Next(4) % 4 == 0)
  52.     //  plus();
  53.     //else if (rand.Next() % 4 == 1)
  54.     //  minus();
  55.     //else if (rand.Next() % 4 == 2)
  56.     //  dividor();
  57.     //else multiplier();
  58.   }
  59.  
  60.   public void plus()
  61.   {
  62.     Console.WriteLine("{0}+{1}={2}", _a, _b, _a + _b);
  63.   }
  64.  
  65.   public void minus()
  66.   {
  67.     Console.WriteLine("{0}-{1}={2}", _a, _b, _a - _b);
  68.   }
  69.  
  70.   public void dividor()
  71.   {
  72.     Console.WriteLine("{0}/{1}={2}", _a, _b, _a / _b);
  73.   }
  74.  
  75.   public void multiplier()
  76.   {
  77.     Console.WriteLine("{0}*{1}={2}", _a, _b, _a * _b);
  78.   }
  79.  
  80.   public int RandomSeed
  81.   {
  82.     set { randomSeed = value; }
  83.   }
  84.  
  85. }
  86. class MainClass
  87. {
  88.   static void Main(string[] args)
  89.   {
  90.     IFoo[] df = new IFoo[10];
  91.     for (int i = 0; i < 10; i++)
  92.     {
  93.       Factory f = new Factory();
  94.  
  95.       // Uncomment only one of the below 3 approaches at a time:
  96.       //SleepingWithRandom(ref f, i);
  97.       LoopingRandom(ref f, i);
  98.       //ComplementingRandom(ref f, i);
  99.  
  100.       df[i] = f;
  101.     }
  102.   }
  103.  
  104.   private static void SleepingWithRandom(ref Factory f, int i)
  105.   {
  106.     // Generate a seed from the current time and set the seed value of the Random to be created.
  107.     f.RandomSeed = unchecked((int)(DateTime.Now.Ticks));
  108.     f.foo(i + 1, i + 1);
  109.  
  110.     // Initiate a delay so as to increase randomness.
  111.     Thread.Sleep(20);
  112.   }
  113.  
  114.   private static void LoopingRandom(ref Factory f, int i)
  115.   {
  116.     // Generate a different seed using the loop variable itself.
  117.     f.RandomSeed = i;
  118.     f.foo(i + 1, i + 1);
  119.   }
  120.  
  121.   private static void ComplementingRandom(ref Factory f, int i)
  122.   {
  123.     // Generate a different seed based on whether the loop variable is even or odd.
  124.     if (i % 2 == 0)
  125.       f.RandomSeed = ~unchecked((int)(DateTime.Now.Ticks));
  126.     else
  127.       f.RandomSeed = unchecked((int)(DateTime.Now.Ticks));
  128.  
  129.     f.foo(i + 1, i + 1);
  130.   }
  131.  
  132. }
Advertisement
Add Comment
Please, Sign In to add comment