C#,数值计算——随机数基类Ran的计算方法与源程序

1 文本格式

using System;

namespace Legalsoft.Truffer
{
    ///


    /// Implementation of the highest quality recommended generator.
    /// The constructor
    /// is called with an integer seed and creates an 
    /// instance of the generator. The
    /// member functions int64, doub, 
    /// and int32 return the next values in the random
    /// sequence, as a variable type indicated by their names.
    /// The period of the
    /// generator is 3.138E57.
    ///

    public class Ran
    {
        private ulong u { get; set; }
        private ulong v { get; set; }
        private ulong w { get; set; }

        ///


        /// Call with any integer seed (except value of v above).
        ///

        ///
        public Ran(ulong j)
        {
            this.v = 4101842887655102017L;
            this.w = 1;
            u = j ^ v; int64();
            v = u; int64();
            w = v; int64();
        }

        ///


        /// Return 64-bit random integer. See text for explanation of method.
        ///

        ///
        public ulong int64()
        {
            u = u * 2862933555777941757L + 7046029254386353087L;
            v ^= v >> 17;
            v ^= v << 31;
            v ^= v >> 8;
            w = 4294957665U * (w & 0xffffffff) + (w >> 32);
            ulong x = u ^ (u << 21);
            x ^= x >> 35;
            x ^= x << 4;
            return (ulong)((x + v) ^ w);
        }

        ///


        /// Return random double-precision floating value in the range 0. to 1.
        ///

        ///
        public double doub()
        {
            double r = 5.42101086242752217E-20 * int64();
            return (r < 0.0) ? (1 + r) : r;
        }

        ///


        /// Return 32-bit random integer.
        ///

        ///
        public uint int32()
        {
            return (uint)int64();
        }
    }
}
 

2 代码格式

using System;

namespace Legalsoft.Truffer
{
    /// 
    /// Implementation of the highest quality recommended generator.
    /// The constructor
    /// is called with an integer seed and creates an 
    /// instance of the generator. The
    /// member functions int64, doub, 
    /// and int32 return the next values in the random
    /// sequence, as a variable type indicated by their names.
    /// The period of the
    /// generator is 3.138E57.
    /// 
    public class Ran
    {
        private ulong u { get; set; }
        private ulong v { get; set; }
        private ulong w { get; set; }

        /// 
        /// Call with any integer seed (except value of v above).
        /// 
        /// 
        public Ran(ulong j)
        {
            this.v = 4101842887655102017L;
            this.w = 1;
            u = j ^ v; int64();
            v = u; int64();
            w = v; int64();
        }

        /// 
        /// Return 64-bit random integer. See text for explanation of method.
        /// 
        /// 
        public ulong int64()
        {
            u = u * 2862933555777941757L + 7046029254386353087L;
            v ^= v >> 17;
            v ^= v << 31;
            v ^= v >> 8;
            w = 4294957665U * (w & 0xffffffff) + (w >> 32);
            ulong x = u ^ (u << 21);
            x ^= x >> 35;
            x ^= x << 4;
            return (ulong)((x + v) ^ w);
        }

        /// 
        /// Return random double-precision floating value in the range 0. to 1.
        /// 
        /// 
        public double doub()
        {
            double r = 5.42101086242752217E-20 * int64();
            return (r < 0.0) ? (1 + r) : r;
        }

        /// 
        /// Return 32-bit random integer.
        /// 
        /// 
        public uint int32()
        {
            return (uint)int64();
        }
    }
}

你可能感兴趣的:(C#数值计算,Numerical,Recipes,c#,数值计算,哈希算法,算法)