Lib/Redzen/Numerics/FastRandom.cs
/// <summary>
/// Fills the provided byte array with random bytes.
/// This method is functionally equivalent to System.Random.NextBytes(). 
/// </summary>
Fills the provided byte array with random bytes.
This method is functionally equivalent to System.Random.NextBytes().
Fills the provided byte array with random bytes . This method is functionally equivalent to System . Random . NextBytes () .
public void NextBytes(byte[] buffer)
        {
            uint x = _x, y = _y, z = _z, w = _w;
            int i = 0;
            uint t;
            for (int bound = buffer.Length - 3; i < bound;)
            {
                t = x ^ (x << 11);
                x = y; y = z; z = w;
                w = (w ^ (w >> 19)) ^ (t ^ (t >> 8));
                buffer[i++] = (byte)w;
                buffer[i++] = (byte)(w >> 8);
                buffer[i++] = (byte)(w >> 16);
                buffer[i++] = (byte)(w >> 24);
            }
            if (i < buffer.Length)
            {
                t = x ^ (x << 11);
                x = y; y = z; z = w;
                w = (w ^ (w >> 19)) ^ (t ^ (t >> 8));
                buffer[i++] = (byte)w;
                if (i < buffer.Length)
                {
                    buffer[i++] = (byte)(w >> 8);
                    if (i < buffer.Length)
                    {
                        buffer[i++] = (byte)(w >> 16);
                        if (i < buffer.Length)
                        {
                            buffer[i] = (byte)(w >> 24);
                        }
                    }
                }
            }
            _x = x; _y = y; _z = z; _w = w;
        }
public void NextBytes ( byte [ ] buffer ) { uint x = _x , y = _y , z = _z , w = _w ; int i = 0 ; uint t ; for ( int bound = buffer . Length - 3 ; i < bound ; ) { t = x ^ ( x << 11 ) ; x = y ; y = z ; z = w ; w = ( w ^ ( w >> 19 ) ) ^ ( t ^ ( t >> 8 ) ) ; buffer [ i ++ ] = ( byte ) w ; buffer [ i ++ ] = ( byte ) ( w >> 8 ) ; buffer [ i ++ ] = ( byte ) ( w >> 16 ) ; buffer [ i ++ ] = ( byte ) ( w >> 24 ) ; } if ( i < buffer . Length ) { t = x ^ ( x << 11 ) ; x = y ; y = z ; z = w ; w = ( w ^ ( w >> 19 ) ) ^ ( t ^ ( t >> 8 ) ) ; buffer [ i ++ ] = ( byte ) w ; if ( i < buffer . Length ) { buffer [ i ++ ] = ( byte ) ( w >> 8 ) ; if ( i < buffer . Length ) { buffer [ i ++ ] = ( byte ) ( w >> 16 ) ; if ( i < buffer . Length ) { buffer [ i ] = ( byte ) ( w >> 24 ) ; } } } } _x = x ; _y = y ; _z = z ; _w = w ; }
Fills the provided byte array with random bytes.
Fills the provided byte array with random bytes .
// Fill up the bulk of the buffer in chunks of 4 bytes at a time. // Generate 4 bytes.  // Increased performance is achieved by generating 4 random bytes per loop. // Also note that no mask needs to be applied to zero out the higher order bytes before // casting because the cast ignores thos bytes. Thanks to Stefan Trosch�tz for pointing this out. // Fill up any remaining bytes in the buffer. // Generate 4 bytes.
buffer
byte[]