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

1 文本格式

using System;

namespace Legalsoft.Truffer
{
    ///


    /// Implements Knuth's subtractive generator using only floating operations. See
    /// text for cautions.
    ///

    public class Ranfib
    {
        private double[] dtab { get; set; } = new double[55];
        private double dd { get; set; }
        private int inext { get; set; }
        private int inextp { get; set; }

        public Ranfib(ulong j)
        {
            this.inext = 0;
            this.inextp = 31;
            Ranq1 init = new Ranq1(j);
            for (int k = 0; k < 55; k++)
            {
                dtab[k] = init.doub();
            }
        }

        public double doub()
        {
            if (++inext == 55)
            {
                inext = 0;
            }
            if (++inextp == 55)
            {
                inextp = 0;
            }
            dd = dtab[inext] - dtab[inextp];
            if (dd < 0)
            {
                dd += 1.0;
            }
            return (dtab[inext] = dd);
        }

        ///


        /// Returns random 32-bit integer. 
        /// Recommended only for testing purposes.
        ///

        ///
        public uint int32()
        {
            return (uint)(doub() * 4294967295.0);
        }
    }
}
 

2 代码格式

using System;

namespace Legalsoft.Truffer
{
    /// 
    /// Implements Knuth's subtractive generator using only floating operations. See
    /// text for cautions.
    /// 
    public class Ranfib
    {
        private double[] dtab { get; set; } = new double[55];
        private double dd { get; set; }
        private int inext { get; set; }
        private int inextp { get; set; }

        public Ranfib(ulong j)
        {
            this.inext = 0;
            this.inextp = 31;
            Ranq1 init = new Ranq1(j);
            for (int k = 0; k < 55; k++)
            {
                dtab[k] = init.doub();
            }
        }

        public double doub()
        {
            if (++inext == 55)
            {
                inext = 0;
            }
            if (++inextp == 55)
            {
                inextp = 0;
            }
            dd = dtab[inext] - dtab[inextp];
            if (dd < 0)
            {
                dd += 1.0;
            }
            return (dtab[inext] = dd);
        }

        /// 
        /// Returns random 32-bit integer. 
        /// Recommended only for testing purposes.
        /// 
        /// 
        public uint int32()
        {
            return (uint)(doub() * 4294967295.0);
        }
    }
}

你可能感兴趣的:(C#数值计算,Numerical,Recipes,c#,数值计算,开发语言,算法)