lib.go 821 Bytes
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28
package random

import (
	"bytes"
	randcrypto "crypto/rand"
	"io"
	randmath "math/rand"
)

func WriteRandomBytes(count int64, w io.Writer) error {
	r := &io.LimitedReader{R: randcrypto.Reader, N: count}
	_, err := io.Copy(w, r)
	return err
}

func WritePseudoRandomBytes(count int64, w io.Writer, seed int64) error {
	randmath.Seed(seed)

	// Configurable buffer size
	bufsize := int64(1024 * 1024 * 4)
	b := make([]byte, bufsize)

	for count > 0 {
		if bufsize > count {
			bufsize = count
			b = b[:bufsize]
		}

Brian Tiger Chow's avatar
Brian Tiger Chow committed
29 30 31 32
		var n uint32
		for i := int64(0); i < bufsize; {
			n = randmath.Uint32()
			for j := 0; j < 4 && i < bufsize; j++ {
33 34 35 36 37 38 39 40 41 42 43 44 45 46 47
				b[i] = byte(n & 0xff)
				n >>= 8
				i++
			}
		}
		count = count - bufsize

		r := bytes.NewReader(b)
		_, err := io.Copy(w, r)
		if err != nil {
			return err
		}
	}
	return nil
}