Exemple #1
0
        /// <summary>
        /// Initializes a new instance of the <see cref="LinearFeedbackShiftRegister32"/> class.
        /// </summary>
        /// <param name="seed">The seed to initialise the sequence with</param>
        public LinearFeedbackShiftRegister32(UInt32 seed)
        {
            mostSignificantBits  = new LinearFeedbackShiftRegister16((UInt16)seed);
            leastSignificantBits = new LinearFeedbackShiftRegister16((UInt16)(seed >> 16));

            repeatThreshold = mostSignificantBits.NextRandom();
            lsb             = leastSignificantBits.NextRandom();
        }
        /// <summary>
        /// Initializes a new instance of the <see cref="LinearFeedbackShiftRegister32"/> class.
        /// </summary>
        /// <param name="seed">The seed to initialise the sequence with</param>
        public LinearFeedbackShiftRegister32(uint seed)
        {
            _mostSignificantBits  = new LinearFeedbackShiftRegister16((ushort)(seed & ushort.MaxValue));
            _leastSignificantBits = new LinearFeedbackShiftRegister16((ushort)((seed >> 16) & ushort.MaxValue));

            _repeatThreshold = _mostSignificantBits.NextRandom();
            _lsb             = _leastSignificantBits.NextRandom();
        }
Exemple #3
0
        /// <summary>
        /// Checks if this is implemented correctly
        /// </summary>
        public static void CorrectnessTest()
        {
            LinearFeedbackShiftRegister16 r = new LinearFeedbackShiftRegister16();

            UInt16 first = r.NextRandom();
            UInt16 value;
            int    period = 0;

            do
            {
                value = r.NextRandom();
                ++period;
            } while (value != first);

            if (period != PERIOD)
            {
                throw new Exception("Period is incorrect");
            }
        }
Exemple #4
0
        /// <summary>
        /// Gets the next random number in the sequence
        /// </summary>
        /// <returns></returns>
        public UInt32 NextRandom()
        {
            UInt16 msb = mostSignificantBits.NextRandom();

            if (msb == repeatThreshold)
            {
                lsb = leastSignificantBits.NextRandom();
            }

            int a = msb << 16 | lsb;

            unsafe
            {
                return(*((UInt32 *)&a));
            }
        }
        /// <summary>
        /// Gets the next random number in the sequence
        /// </summary>
        /// <returns></returns>
        public uint NextRandom()
        {
            ushort msb = _mostSignificantBits.NextRandom();

            if (msb == _repeatThreshold)
            {
                _lsb = _leastSignificantBits.NextRandom();
            }

            int a = msb << 16 | _lsb;

            unsafe
            {
                return(*((uint *)&a));
            }
        }