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

1 文本格式

using System;

namespace Legalsoft.Truffer
{
    ///


    /// Generator for random bytes using the algorithm generally known as RC4.
    ///

    public class Ranbyte
    {
        private int[] s { get; set; } = new int[256];
        private int i { get; set; }
        private int j { get; set; }
        private int ss { get; set; }
        private uint v { get; set; }

        public Ranbyte(int u)
        {
            v = (uint)(2244614371U ^ u);
            for (i = 0; i < 256; i++)
            {
                s[i] = i;
            }
            for (j = 0, i = 0; i < 256; i++)
            {
                ss = s[i];
                j = (int)((j + ss + (v >> 24)) & 0xff);
                s[i] = s[j];
                s[j] = ss;
                v = (uint)((v << 24) | (v >> 8));
            }
            i = j = 0;
            for (int k = 0; k < 256; k++)
            {
                int8();
            }
        }

        public byte int8()
        {
            i = (i + 1) & 0xff;
            ss = s[i];
            j = (j + ss) & 0xff;
            s[i] = s[j];
            s[j] = ss;
            return (byte)(s[(s[i] + s[j]) & 0xff]);
        }

        ///


        /// Returns a random 32-bit integer constructed from 4 random bytes. Slow!
        ///

        ///
        public uint int32()
        {
            v = 0;
            for (int k = 0; k < 4; k++)
            {
                i = (i + 1) & 0xff;
                ss = s[i];
                j = (j + ss) & 0xff;
                s[i] = s[j];
                s[j] = ss;
                v = (uint)((uint)(v << 8) | (uint)(s[(s[i] + s[j]) & 0xff]));
            }
            return v;
        }

        ///


        /// Returns a random double-precision floating value between 0. and 1. Slow!!
        ///

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

2 代码格式

using System;

namespace Legalsoft.Truffer
{
    /// 
    /// Generator for random bytes using the algorithm generally known as RC4.
    /// 
    public class Ranbyte
    {
        private int[] s { get; set; } = new int[256];
        private int i { get; set; }
        private int j { get; set; }
        private int ss { get; set; }
        private uint v { get; set; }

        public Ranbyte(int u)
        {
            v = (uint)(2244614371U ^ u);
            for (i = 0; i < 256; i++)
            {
                s[i] = i;
            }
            for (j = 0, i = 0; i < 256; i++)
            {
                ss = s[i];
                j = (int)((j + ss + (v >> 24)) & 0xff);
                s[i] = s[j];
                s[j] = ss;
                v = (uint)((v << 24) | (v >> 8));
            }
            i = j = 0;
            for (int k = 0; k < 256; k++)
            {
                int8();
            }
        }

        public byte int8()
        {
            i = (i + 1) & 0xff;
            ss = s[i];
            j = (j + ss) & 0xff;
            s[i] = s[j];
            s[j] = ss;
            return (byte)(s[(s[i] + s[j]) & 0xff]);
        }

        /// 
        /// Returns a random 32-bit integer constructed from 4 random bytes. Slow!
        /// 
        /// 
        public uint int32()
        {
            v = 0;
            for (int k = 0; k < 4; k++)
            {
                i = (i + 1) & 0xff;
                ss = s[i];
                j = (j + ss) & 0xff;
                s[i] = s[j];
                s[j] = ss;
                v = (uint)((uint)(v << 8) | (uint)(s[(s[i] + s[j]) & 0xff]));
            }
            return v;
        }

        /// 
        /// Returns a random double-precision floating value between 0. and 1. Slow!!
        /// 
        /// 
        public double doub()
        {
            return 2.32830643653869629E-10 * (int32() + 2.32830643653869629E-10 * int32());
        }
    }
}

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