github.com/cznic/mathutil@v0.0.0-20181122101859-297441e03548/example2/example2.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 "bytes" 13 "github.com/cznic/mathutil" 14 "image" 15 "image/png" 16 "io/ioutil" 17 "log" 18 "math" 19 "math/rand" 20 ) 21 22 // $ go run example2.go # view rand.png and rnd.png by your favorite pic viewer 23 // 24 // see http://www.boallen.com/random-numbers.html 25 func main() { 26 sqr := image.Rect(0, 0, 511, 511) 27 r, err := mathutil.NewFC32(math.MinInt32, math.MaxInt32, true) 28 if err != nil { 29 log.Fatal("NewFC32", err) 30 } 31 32 img := image.NewGray(sqr) 33 for y := 0; y < 512; y++ { 34 for x := 0; x < 512; x++ { 35 if r.Next()&1 != 0 { 36 img.Set(x, y, image.White) 37 } 38 } 39 } 40 buf := bytes.NewBuffer(nil) 41 if err := png.Encode(buf, img); err != nil { 42 log.Fatal("Encode rnd.png ", err) 43 } 44 45 if err := ioutil.WriteFile("rnd.png", buf.Bytes(), 0666); err != nil { 46 log.Fatal("ioutil.WriteFile/rnd.png ", err) 47 } 48 49 r2 := rand.New(rand.NewSource(0)) 50 img = image.NewGray(sqr) 51 for y := 0; y < 512; y++ { 52 for x := 0; x < 512; x++ { 53 if r2.Int()&1 != 0 { 54 img.Set(x, y, image.White) 55 } 56 } 57 } 58 buf = bytes.NewBuffer(nil) 59 if err := png.Encode(buf, img); err != nil { 60 log.Fatal("Encode rand.png ", err) 61 } 62 63 if err := ioutil.WriteFile("rand.png", buf.Bytes(), 0666); err != nil { 64 log.Fatal("ioutil.WriteFile/rand.png ", err) 65 } 66 }