On again i´m needing some help here, today i was doin a c# homework that basically consist in creating a method that can sum the result of 2 dice rolls, so far, so good!, no hard thing to do... BUT, when i run my code some really odd thing happens, i will let my code and results to speak from them self:
using System;
class Program
{
public static String GetTimestamp()
{
DateTime saveNow = DateTime.Now;
return saveNow.ToString("yyyyMMddHHmmssffff");
}
public static int ThrowDice()
{
int randVal1, randVal2;
string TS1, TS2;
Random rand = new Random();
randVal1 = rand.Next(1,6);
TS1 = GetTimestamp();
randVal2 = rand.Next(1,6);
TS2 = GetTimestamp();
Console.WriteLine("Dice 1: \t" + randVal1 + ' ' + TS1);
Console.WriteLine("Dice 2: \t" + randVal2 + ' ' + TS2);
Console.WriteLine("-----------------");
return randVal1 + randVal2;
}
static void Main()
{
for(int i = 1; i<=10;i++)
{
Console.WriteLine("Dice Roll #" + i);
Console.WriteLine("Dice Sum:\t" + ThrowDice());
Console.WriteLine();
}
}
}
Now, this is what i got when i ran it:
Dice Roll #1
Dice 1: 1 / TimeStamp : 201308260146259075
Dice 2: 2 / TimeStamp : 201308260146259131
Dice Sum: 3
Dice Roll #2
Dice 1: 2 / TimeStamp : 201308260146259135
Dice 2: 5 / TimeStamp : 201308260146259135
Dice Sum: 7
Dice Roll #3
Dice 1: 2 / TimeStamp : 201308260146259135
Dice 2: 5 / TimeStamp : 201308260146259135
Dice Sum: 7
Dice Roll #4
Dice 1: 2 / TimeStamp : 201308260146259135
Dice 2: 5 / TimeStamp : 201308260146259135
Dice Sum: 7
Dice Roll #5
Dice 1: 2 / TimeStamp : 201308260146259135
Dice 2: 5 / TimeStamp : 201308260146259135
Dice Sum: 7
Dice Roll #6
Dice 1: 2 / TimeStamp : 201308260146259135
Dice 2: 5 / TimeStamp : 201308260146259135
Dice Sum: 7
Dice Roll #7
Dice 1: 2 / TimeStamp : 201308260146259135
Dice 2: 5 / TimeStamp : 201308260146259135
Dice Sum: 7
Dice Roll #8
Dice 1: 2 / TimeStamp : 201308260146259135
Dice 2: 5 / TimeStamp : 201308260146259135
Dice Sum: 7
Dice Roll #9
Dice 1: 2 / TimeStamp : 201308260146259135
Dice 2: 5 / TimeStamp : 201308260146259135
Dice Sum: 7
Dice Roll #10
Dice 1: 2 / TimeStamp : 201308260146259135
Dice 2: 5 / TimeStamp : 201308260146259135
Dice Sum: 7
-Roll 1 is perfect
-Roll 2/dice 1... perfect,
-Roll 2/dice 2... WTH???
Those results are really weird, i was expecting to get different timestamps and different values on each roll. The timeStamp stuff what an idea i came up with to check the time when those numbers are generated, and it looks like both "dices" are rolled at the same time, it looks like "Random" generates the seed using the clock.
I even tryed creating a whole class for dice with a DiceRoll() method, but even there where both a complete different objects i was just getting the same results over and over..
How can i fix this?, any ideas?