File: example2.go

package info (click to toggle)
golang-github-cznic-mathutil 1.7.1-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 1,480 kB
  • sloc: makefile: 59
file content (67 lines) | stat: -rw-r--r-- 1,459 bytes parent folder | download
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
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
// Copyright (c) 2011 CZ.NIC z.s.p.o. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.

// blame: jnml, labs.nic.cz

//go:build ignore
// +build ignore

package main

import (
	"bytes"
	"image"
	"image/png"
	"io/ioutil"
	"log"
	"math"
	"math/rand"
	"modernc.org/mathutil"
)

// $ go run example2.go # view rand.png and rnd.png by your favorite pic viewer
//
// see http://www.boallen.com/random-numbers.html
func main() {
	sqr := image.Rect(0, 0, 511, 511)
	r, err := mathutil.NewFC32(math.MinInt32, math.MaxInt32, true)
	if err != nil {
		log.Fatal("NewFC32", err)
	}

	img := image.NewGray(sqr)
	for y := 0; y < 512; y++ {
		for x := 0; x < 512; x++ {
			if r.Next()&1 != 0 {
				img.Set(x, y, image.White)
			}
		}
	}
	buf := bytes.NewBuffer(nil)
	if err := png.Encode(buf, img); err != nil {
		log.Fatal("Encode rnd.png ", err)
	}

	if err := ioutil.WriteFile("rnd.png", buf.Bytes(), 0666); err != nil {
		log.Fatal("ioutil.WriteFile/rnd.png ", err)
	}

	r2 := rand.New(rand.NewSource(0))
	img = image.NewGray(sqr)
	for y := 0; y < 512; y++ {
		for x := 0; x < 512; x++ {
			if r2.Int()&1 != 0 {
				img.Set(x, y, image.White)
			}
		}
	}
	buf = bytes.NewBuffer(nil)
	if err := png.Encode(buf, img); err != nil {
		log.Fatal("Encode rand.png ", err)
	}

	if err := ioutil.WriteFile("rand.png", buf.Bytes(), 0666); err != nil {
		log.Fatal("ioutil.WriteFile/rand.png ", err)
	}
}