Redzen.Numerics.XorShiftRandom.NextByte C# (CSharp) Method

NextByte() public method

Generates a signle random byte with range [0,255]. This method's performance is improved by generating 4 bytes in one operation and storing them ready for future calls.
public NextByte ( ) : byte
return byte
        public byte NextByte()
        {
            if(0 == _byteBufferState)
            {
                // Generate 4 more bytes.
                uint t = _x^(_x<<11);
                _x=_y; _y=_z; _z=_w;
                _byteBuffer = _w=(_w^(_w>>19))^(t^(t>>8));
                _byteBufferState = 0x4;
                return (byte)_byteBuffer;  // Note. Masking with 0xFF is unnecessary.
            }
            _byteBufferState >>= 1;
            return (byte)(_byteBuffer >>= 8);
        }

Usage Example

Example #1
0
        public void NextByte()
        {
            int sampleCount = 10000000;
            XorShiftRandom rng = new XorShiftRandom();
            byte[] sampleArr = new byte[sampleCount];
            for(int i=0; i<sampleCount; i++){
                sampleArr[i] = rng.NextByte();

            }
            NextByteInner(sampleArr);
        }