C#, numerical calculation - calculation method and source program of Ranlim32

1 text format

using System;

namespace Legalsoft.Truffer
{
    /// <summary>
    /// High-quality random generator using only 32-bit arithmetic.Same conventions
    /// as Ran.Period is 3.11E37 . Recommended only when 64-bit arithmetic is not
    /// available.
    /// </summary>
    public class Ranlim32
    {
        private uint u { get; set; }
        private uint v { get; set; } = 0x85ca18e3; // 0x85ca18e3=2244614371;
        private uint w1 { get; set; } = 521288629;
        private uint w2 { get; set; } = 362436069;

        public Ranlim32(uint j)
        {
            this.v = 2244614371U;
            this.w1 = 521288629U;
            this.w2 = 362436069U;
            u = j ^ v; int32();
            v = u; int32();
        }

        public uint int32()
        {
            u = u * 2891336453U + 1640531513U;
            v ^= v >> 13;
            v ^= v << 17;
            v ^= v >> 5;
            w1 = (uint)(33378 * (w1 & 0xffff) + (w1 >> 16));
            w2 = (uint)(57225 * (w2 & 0xffff) + (w2 >> 16));
            uint x = u ^ (u << 9);
            x ^= x >> 17;
            x ^= x << 6;
            uint y = w1 ^ (w1 << 17);
            y ^= y >> 15;
            y ^= y << 5;
            return (x + v) ^ (y + w2);
        }

        public double doub()
        {
            double r = 2.32830643653869629E-10 * int32();
            return (r < 0.0) ? (1 + r) : r;
        }

        public double truedoub()
        {
            return 2.32830643653869629E-10 * (int32() + 2.32830643653869629E-10 * int32());
        }
    }
}
 

2 code format

using System;

namespace Legalsoft.Truffer
{
    /// <summary>
    /// High-quality random generator using only 32-bit arithmetic.Same conventions
    /// as Ran.Period is 3.11E37 . Recommended only when 64-bit arithmetic is not
    /// available.
    /// </summary>
    public class Ranlim32
    {
        private uint u { get; set; }
        private uint v { get; set; } = 0x85ca18e3; // 0x85ca18e3=2244614371;
        private uint w1 { get; set; } = 521288629;
        private uint w2 { get; set; } = 362436069;

        public Ranlim32(uint j)
        {
            this.v = 2244614371U;
            this.w1 = 521288629U;
            this.w2 = 362436069U;
            u = j ^ v; int32();
            v = u; int32();
        }

        public uint int32()
        {
            u = u * 2891336453U + 1640531513U;
            v ^= v >> 13;
            v ^= v << 17;
            v ^= v >> 5;
            w1 = (uint)(33378 * (w1 & 0xffff) + (w1 >> 16));
            w2 = (uint)(57225 * (w2 & 0xffff) + (w2 >> 16));
            uint x = u ^ (u << 9);
            x ^= x >> 17;
            x ^= x << 6;
            uint y = w1 ^ (w1 << 17);
            y ^= y >> 15;
            y ^= y << 5;
            return (x + v) ^ (y + w2);
        }

        public double doub()
        {
            double r = 2.32830643653869629E-10 * int32();
            return (r < 0.0) ? (1 + r) : r;
        }

        public double truedoub()
        {
            return 2.32830643653869629E-10 * (int32() + 2.32830643653869629E-10 * int32());
        }
    }
}

Guess you like

Origin blog.csdn.net/beijinghorn/article/details/133418975