github.com/cznic/mathutil@v0.0.0-20181122101859-297441e03548/example3/example3.go (about)

     1  // Copyright (c) 2011 CZ.NIC z.s.p.o. All rights reserved.
     2  // Use of this source code is governed by a BSD-style
     3  // license that can be found in the LICENSE file.
     4  
     5  // blame: jnml, labs.nic.cz
     6  
     7  // +build ignore
     8  
     9  package main
    10  
    11  import (
    12  	"bufio"
    13  	"flag"
    14  	"log"
    15  	"math/rand"
    16  	"os"
    17  )
    18  
    19  /*
    20  
    21  $ # Usage e.g.:
    22  $ go run example3.go -max 1024 > rand.dat # generate 1kB of "random" data
    23  
    24  */
    25  func main() {
    26  	r := rand.New(rand.NewSource(1))
    27  	var mflag uint64
    28  	flag.Uint64Var(&mflag, "max", 0, "limit output to max bytes")
    29  	flag.Parse()
    30  	stdout := bufio.NewWriter(os.Stdout)
    31  	if mflag != 0 {
    32  		for i := uint64(0); i < mflag; i++ {
    33  			if err := stdout.WriteByte(byte(r.Int())); err != nil {
    34  				log.Fatal(err)
    35  			}
    36  		}
    37  		stdout.Flush()
    38  		return
    39  	}
    40  
    41  	for stdout.WriteByte(byte(r.Int())) == nil {
    42  	}
    43  }