BattleNet.Utils.RandomString C# (CSharp) Method

RandomString() public static method

public static RandomString ( int size ) : string
size int
return string
        public static string RandomString(int size)
        {
            StringBuilder builder = new StringBuilder();
            Random random = new Random();
            char ch;
            for (int i = 0; i < size; i++)
            {
                ch = Convert.ToChar(Convert.ToInt32(Math.Floor(26 * random.NextDouble() + 97 )));
                if (i == 0)
                    builder.Append(Char.ToUpper(ch));
                else
                    builder.Append(ch);
            }

            return builder.ToString();
        }

Usage Example

        public void MakeGame()
        {
            if (Password.Length == 0)
            {
                Password = "******";
            }

            GameName = Utils.RandomString(10);
            if (FailedGame)
            {
                Console.WriteLine("{0}: [BNCS] Last game failed, sleeping.", Account);
                System.Threading.Thread.Sleep(30000);
            }

            // We assume the game fails every game, until it proves otherwise at end of botthread.
            FailedGame = true;

            Console.WriteLine("{0}: [MCP] Creating game \"{1}\" with password \"{2}\"", Account, GameName, GamePassword);

            byte[] temp   = { 0x01, 0xff, 0x08 };
            byte[] packet = m_mcp.BuildPacket(0x03, BitConverter.GetBytes((UInt16)GameRequestId), BitConverter.GetBytes(Utils.GetDifficulty(Difficulty)), temp, System.Text.Encoding.ASCII.GetBytes(GameName), GenericServer.zero,
                                              System.Text.Encoding.ASCII.GetBytes(GamePassword), GenericServer.zero, GenericServer.zero);

            m_mcp.Write(packet);
            GameRequestId++;
        }