示例#1
0
文件: Math.cs 项目: kendallb/peachpie
        /// <summary>
        /// Generate a unique ID.
        /// </summary>
        /// <remarks>
        /// With an empty prefix, the returned string will be 14 characters long. If more_entropy is TRUE, it will be 23 characters.
        /// </remarks>
        /// <param name="prefix">Use the specified prefix.</param>
        /// <param name="more_entropy">Use LCG to generate a random postfix.</param>
        /// <returns>A pseudo-random string composed from the given prefix, current time and a random postfix.</returns>
        public static string uniqid(string prefix = "", bool more_entropy = false)
        {
            // Note that Ticks specify time in 100nanoseconds but it is raised each 100144
            // ticks which is around 10 times a second (the same for Milliseconds).

            const int tickslength = 14;

            // 14 digits, hexadecimal, lowercased
            var ticks = ((ulong)(System.DateTime.UtcNow.Ticks + MTGenerator.Next()))
                        .ToString("x" /*x14*/, CultureInfo.InvariantCulture);

            if (ticks.Length > tickslength)
            {
                ticks = ticks.Remove(tickslength);
            }
            else if (ticks.Length < tickslength)
            {
                ticks = ticks.PadLeft(tickslength, '0');
            }

            if (more_entropy)
            {
                // 8 digits from the lcg:
                var rnd = ((ulong)(lcg_value() * 100_000_000)).ToString("d8", CultureInfo.InvariantCulture);
                return(prefix + ticks + "." + rnd);
            }
            else
            {
                return(prefix + ticks);
            }
        }
示例#2
0
        /// <summary>
        /// Generate a unique ID.
        /// </summary>
        /// <remarks>
        /// With an empty prefix, the returned string will be 13 characters long. If more_entropy is TRUE, it will be 23 characters.
        /// </remarks>
        /// <param name="prefix">Use the specified prefix.</param>
        /// <param name="more_entropy">Use LCG to generate a random postfix.</param>
        /// <returns>A pseudo-random string composed from the given prefix, current time and a random postfix.</returns>
        public static string uniqid(string prefix, bool more_entropy)
        {
            // Note that Ticks specify time in 100nanoseconds but it is raised each 100144
            // ticks which is around 10 times a second (the same for Milliseconds).
            string ticks = string.Format("{0:X}", System.DateTime.UtcNow.Ticks + MTGenerator.Next());

            ticks = ticks.Substring(ticks.Length - 13);
            if (prefix == null)
            {
                prefix = "";
            }
            if (more_entropy)
            {
                string rnd = lcg_value().ToString();
                rnd = rnd.Substring(2, 8);
                return(string.Format("{0}{1}.{2}", prefix, ticks, rnd));
            }
            else
            {
                return(string.Format("{0}{1}", prefix, ticks));
            }
        }
示例#3
0
 public static int mt_rand(int min, int max)
 {
     return((min < max) ? MTGenerator.Next(min, max) : MTGenerator.Next(max, min));
 }
示例#4
0
 public static int mt_rand()
 {
     return(MTGenerator.Next());
 }
示例#5
0
文件: Math.cs 项目: kendallb/peachpie
 public static int mt_rand(int min, int max) // TODO: long min, long max, mt_getrandmax
 {
     return((min < max) ? MTGenerator.Next(min, max) : MTGenerator.Next(max, min));
 }