Code:
```//Write a program that simulates a "loaded" die that comes up with the value 1 for half the time
//and some other random value the other half the time.

//This is an exercise from Chapter 2 of book "Microsoft C# programming for the absolute beginner".
//This example is in no way the most elegant solution. It was written strictly for illustrative
//purpose.
using System;

{
class Program
{
static void Main(string[] args)
{
Console.WriteLine("\nRolling the normal die:");
NormalDie normal = new NormalDie();
Console.WriteLine(normal);
Console.WriteLine("\nConclusion: try not to play against somebody with a loaded die");

}
}
{
//Some field data to see how many ones....sixes we get out of a thousand die rolls
public int ones;
public int twos;
public int threes;
public int fours;
public int fives;
public int sixes;
public int[] dies; //keep the results of die rolls

{
Random r = new Random(); //get Random object
double raw; //will hold the result of r.NextDouble()
int die; //will hold the casted raw
dies = new int[1000];
for (int i = 0; i < 1000; i++) //roll the die 1000 times
{
raw = r.NextDouble(); //get a value in the range [0.0:1.0]
if (raw <= 0.5) //here is the "loaded" logic. In english: load the die to return a given value approximately half of the time
{
dies[i] = die = 1; //the "loaded" value
ones++; //increase the ones count
}
else //what to do with the rest half of the time
{
raw = r.NextDouble(); //regenerate random number. If we would use the previous value of raw, we would lose some values
die = (int)(raw * 6) + 1; //cast the raw to a value in the range [1:6]
dies[i] = die; //store the result
switch (die) //see what value die roll returned, adjust corresponding field
{
case 1: ones++; break;
case 2: twos++; break;
case 3: threes++; break;
case 4: fours++; break;
case 5: fives++; break;
case 6: sixes++; break;
}
}
}
}

public override string ToString() //override ToString() to make it easy to see the results of the 1000 rolls
{
return string.Format("There were {0} rolls\n1's: {1}\n2's: {2}\n3's: {3}\n4's: {4}\n5's: {5}\n6's: {6}",
dies.Length, ones, twos, threes, fours, fives, sixes);
}
}

class NormalDie
{
public int ones;
public int twos;
public int threes;
public int fours;
public int fives;
public int sixes;
public int[] dies;

public NormalDie()
{
Random r = new Random();
double raw;
int die;
dies = new int[1000];
for (int i = 0; i < 1000; i++)
{
raw = r.NextDouble();
die = (int)(raw * 6) + 1;
dies[i] = die;
switch (die)
{
case 1: ones++; break;
case 2: twos++; break;
case 3: threes++; break;
case 4: fours++; break;
case 5: fives++; break;
case 6: sixes++; break;
}
}
}

public override string ToString()
{
return string.Format("There were {0} rolls\n1's: {1}\n2's: {2}\n3's: {3}\n4's: {4}\n5's: {5}\n6's: {6}",
dies.Length, ones, twos, threes, fours, fives, sixes);
}
}
}```

Last edited by shabbir; 16Jan2011 at 12:20.. Reason: Code blocks