Entries:
Comments:
Posts:

Loading User Information from Channel 9

Something went wrong getting user information from Channel 9

Latest Achievement:

Loading User Information from MSDN

Something went wrong getting user information from MSDN

Visual Studio Achievements

Latest Achievement:

Loading Visual Studio Achievements

Something went wrong getting the Visual Studio Achievements

Discussions

jj5 jj5 Yeah. We got goth served.
  • What is your favorite search engine: And why?

    I use Mamma.

    I get excellent results from this tool.

    I made a deliberate decision to stop using google, because it is regulated in a country that passed the Patriot Act, and I'm concerned about the motivation behind services such as g-mail and orkut. To me, they seem to gather a vast amount of information about human networks and the content of human communications. Google seems particularly interested in this, and whether the motivation is 'commercial', 'evil', or 'otherwise', I'm not interested in supporting their success.

    Call me paranoid, I don't use google.

    John.

  • Bad OOD in MS .NET Framework 1.1 ?!

    The Liskov Substitution Principle is broken in many places in the framework.

    This is a problem that generics might help to solve?

  • Microsoft granted patent on double-click

    Two words: MORSE CODE.

    John.

  • What is Microsoft doing about GMail?

    MS is probably just waiting patiently for google to set the precident that it's OK to spy on people by reading their e-mail, then start doing it themselves.

    Gmail is evil. Big Brother is watching you, etc.

    John.

  • Tough SQL Server XML Challenge

    Erm, who is Rumpi Gravenstein and how do they feel about you posting this information to this forum?

    John.

  • C# dice Roller

    St23aM wrote:
    You said "on each loop add the current roll to the end of the string, and then tack the total on after the loop exits" This is exactly what im trying to figure out how to do. Could you make a suggestion in code?


    This is pretty much what the code I posted does. I guess I should have commented it for you..

    This time with comments.

    John.

      using System;
      using System.Text;

      public class EntryPoint {

        [STAThread]
        public static void Main(string[] args) {

          // roll two die with 6 faces
          Console.WriteLine(Dice.Roll(2, 6));

          // roll 4 die with 8 faces
          Console.WriteLine(Dice.Roll(4,Eye Rolling);

          Console.Write("Press ENTER to exit.");
          Console.ReadLine();

        }
      }

      public class Dice {

        /// <summary>
        /// Rolls the specified number of die each with the specified number of
        /// sides and returns the result as a string, including the total.
        /// </summary>
        /// <param name="numberOfDice">The number of die to roll.</param>
        /// <param name="numberOfSides">The number of faces on each dice rolled.</param>
        /// <returns>A string containing the result of the roll.</returns>
        public static String Roll(Int32 numberOfDice, Int32 numberOfSides) {

          // don't allow a Number of Dice less than or equal to zero
          if (numberOfDice <= 0) {
            throw new ApplicationException("Number of die must be greater than zero.");
          }

          // don't allow a Number of Sides less than or equal to zero
          if (numberOfSides <= 0) {
            throw new ApplicationException("Number of sides must be greater than zero.");
          }

          // Create the random class used to generate random numbers.
          // See: http://msdn.microsoft.com/library/default.asp?url=/library/en-us/cpref/html/frlrfSystemRandomClassTopic.asp
          Random rnd = new Random((Int32)DateTime.Now.Ticks);

          // Create the string builder class used to build the string
          // we return with the result of the die rolls.
          // See: http://msdn.microsoft.com/library/default.asp?url=/library/en-us/cpref/html/frlrfsystemtextstringbuilderclasstopic.asp
          StringBuilder result = new StringBuilder();

          // Declare the integer in which we will keep the total of the rolls
          Int32 total = 0;

          // repeat once for each number of dice
          for (Int32 i = 0; i < numberOfDice; i++) {

            // Get a pseudo-random result for this roll
            Int32 roll = rnd.Next(1, numberOfSides);

            // Add the result of this roll to the total
            total += roll;

            // Add the result of this roll to the string builder
            result.AppendFormat("Dice {0:00}:\t{1}\n", i + 1, roll);

          }

          // Add a line to the result to seperate the rolls from the total
          result.Append("\t\t--\n");

          // Add the total to the result
          result.AppendFormat("TOTAL:\t\t{0}\n", total);

          // Now that we've finished building the result, get the string
          // that we've been building and return it.
          return result.ToString();

        }
      }

  • C# dice Roller

    Or, if you're not planning to eventually do something with that array, you could just do it this way. You could seed the random class with something more exotic (if you really care, if you're an online casino then you *really* care).

    John.

      using System;
      using System.Text;

      public class EntryPoint {

        [STAThread]
        public static void Main(string[] args) {

          Console.WriteLine(Dice.Roll(2, 6));
          Console.WriteLine(Dice.Roll(4,Eye Rolling);
          Console.Write("Press ENTER to exit.");
          Console.ReadLine();

        }
      }

      public class Dice {

        public static String Roll(Int32 numberOfDice, Int32 numberOfSides) {

          if (numberOfDice <= 0) {
            throw new ApplicationException("Number of die must be greater than zero.");
          }

          if (numberOfSides <= 0) {
            throw new ApplicationException("Number of sides must be greater than zero.");
          }

          Random rnd = new Random((Int32)DateTime.Now.Ticks);
          StringBuilder result = new StringBuilder();
          Int32 total = 0;

          for (Int32 i = 0; i < numberOfDice; i++) {

            Int32 roll = rnd.Next(1, numberOfSides);
            total += roll;
            result.AppendFormat("Dice {0:00}:\t{1}\n", i + 1, roll);

          }

          result.Append("\t\t--\n");
          result.AppendFormat("TOTAL:\t\t{0}\n", total);

          return result.ToString();

        }
      }

  • C# dice Roller

    How about something like this. You might like to play around with the formatting of the output.

    John.

      using System;
      using System.Text;

      public class EntryPoint {

        [STAThread]
        public static void Main(string[] args) {

          Console.WriteLine(Dice.Roll(2, 6));
          Console.Write("Press ENTER to exit.");
          Console.ReadLine();

        }
      }

      public class Dice {

        public static String Roll(Int32 numberOfDice, Int32 numberOfSides) {

          if (numberOfDice <= 0) {
            throw new ApplicationException("Number of die must be greater than zero.");
          }

          if (numberOfSides <= 0) {
            throw new ApplicationException("Number of sides must be greater than zero.");
          }

          Random rnd = new Random((Int32)DateTime.Now.Ticks);

          Int32[] roll = new Int32[numberOfDice];

          for (Int32 i = 0; i < numberOfDice; i++) {

            roll[i] = rnd.Next(1, numberOfSides);

          }

          StringBuilder result = new StringBuilder();
          Int32 total = 0;

          for (Int32 i = 0; i < roll.Length; i++) {

            total += roll[i];
            result.AppendFormat("Dice {0:00}:\t{1}\n", i + 1, roll[i]);

          }

          result.AppendFormat("\t\t--\n");
          result.AppendFormat("TOTAL:\t\t{0}\n", total);

          return result.ToString();

        }

      }

  • Error Handling, use exceptions?

    For my money all the TryParse functionality should be moved into an 'advanced' class, such as System.Text.FastParser for example.

    All the BCL value types that support parsing should have a Parse method that throws and an IsInvalid(String) method. (I like to test for 'invalid' because that tends to be the exceptional case, but I'd be happy enough with IsValid(..)).

    Keep the common APIs simple, clean, and consistent. Having TryParse on Int32 is just ugly.

    John.

  • Error Handling, use exceptions?

    Hey spod,

    spod wrote:
    there was a long and interesting argument about this internally. The main reason for not doing

    if( IsParsable( String ) )
       int i = Int.Parse(string );

    is that it's hard to avoid a double parse of the string, which is too much of a perf hit in some cases...


    Bah! I knew you'd say that. I think that the wrong decision was made.

    I'd prefer to have Boolean Int32.IsInvalid(String) and Int32 Int32.Parse(String).

    If you want performance then you wouldn't check for invalid data, you'd assume it was valid and let the exception fall out.

    I guess there is a case for TryParse, although every bone in my body wants to deny it. I imagine this would typically be used in UI code (e.g. to get an Int32 value from a TextBox) in this case the performance hit of double parsing is negligable. If you were trying to get an Int32 value out of a text file or what have you, it would be better (in my opinion) to insist that your data was valid and let the exception fall out (so you wouldn't check at all).

    What are the cases where TryParse(String, ref Int32) significantly helps performance?

    Why not support all methods? I.e. have an IsInvalid/IsValid/IsParseable query, a Parse method that throws and TryParse as is?

    John.