package main

import "math/rand"

func Random(length int, bytes []byte, write func([]byte)) {

	b := len(bytes)

	// We don't want to call stdout too often, so we will generate the output in chunks.
	for {

		size := 1024

		if size > length {
			size = length
		}

		length -= size

		buf := make([]byte, size)

		for j := 0; j < size; j++ {
			buf[j] = bytes[rand.Intn(b)]
		}

		write(buf)

		if length == 0 {
			break
		}

	}

}