当前位置:首页 > 编程笔记 > 正文
已解决

C#,数值计算——Ranlim32的计算方法与源程序

来自网友在路上 158858提问 提问时间:2023-10-02 09:01:22阅读次数: 58

最佳答案 问答题库588位专家为你答疑解惑

1 文本格式

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 代码格式

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());}}
}

查看全文

99%的人还看了

猜你感兴趣

版权申明

本文"C#,数值计算——Ranlim32的计算方法与源程序":http://eshow365.cn/6-15789-0.html 内容来自互联网,请自行判断内容的正确性。如有侵权请联系我们,立即删除!