Generate Random Mobile Number - CSharp System

CSharp examples for System:Random

Description

Generate Random Mobile Number

Demo Code


using System.Text;
using System.Security.Cryptography;
using System;//  w  ww  .  jav  a  2  s . c o  m

public class Main{
        public static string GenerateRandomMobileNumber()
        {
            return string.Format("04{0}", GeneratRandomNumber(8));
        }
        /// <summary>
        /// Create a random number as a string with a maximum length.
        /// </summary>
        /// <param name="length">Length of number</param>
        /// <returns>Generated string</returns>
        public static string GeneratRandomNumber(int length)
        {
            if (length > 0)
            {
                var sb = new StringBuilder();

                var rnd = SeedRandom();
                for (int i = 0; i < length; i++)
                {
                    sb.Append(rnd.Next(0, 9).ToString());
                }

                return sb.ToString();
            }

            return string.Empty;
        }
        private static Random SeedRandom()
        {
            return new Random(Guid.NewGuid().GetHashCode());
        }
}

Related Tutorials