コード例 #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
 /// <summary>
 /// Seed the better random number generator.
 /// No return value.
 /// </summary>
 /// <param name="seed">Optional seed value.</param>
 public static void mt_srand(int seed)
 {
     MTGenerator.Seed(unchecked ((uint)seed));
 }
コード例 #4
0
 public static int mt_rand(int min, int max)
 {
     return((min < max) ? MTGenerator.Next(min, max) : MTGenerator.Next(max, min));
 }
コード例 #5
0
 public static int mt_rand()
 {
     return(MTGenerator.Next());
 }
コード例 #6
0
 /// <summary>
 /// Generates a pseudo-random number using linear congruential generator in the range of (0,1).
 /// </summary>
 /// <remarks>
 /// This method uses the Framwork <see cref="rand()"/> generator
 /// which may or may not be the same generator as the PHP one (L(CG(2^31 - 85),CG(2^31 - 249))).
 /// </remarks>
 /// <returns></returns>
 public static double lcg_value() => MTGenerator.NextDouble();
コード例 #7
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));
 }