~ubuntu-branches/ubuntu/vivid/juju-core/vivid-updates

« back to all changes in this revision

Viewing changes to src/github.com/ajstarks/svgo/randcomp/randcomp.go

  • Committer: Package Import Robot
  • Author(s): Curtis C. Hovey
  • Date: 2015-09-29 19:43:29 UTC
  • mfrom: (47.1.4 wily-proposed)
  • Revision ID: package-import@ubuntu.com-20150929194329-9y496tbic30hc7vp
Tags: 1.24.6-0ubuntu1~15.04.1
Backport of 1.24.6 from wily. (LP: #1500916, #1497087)

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
// randcomp visualizes random number generators
 
2
// +build !appengine
 
3
 
 
4
package main
 
5
 
 
6
import (
 
7
        "fmt"
 
8
        "math/rand"
 
9
        "os"
 
10
        "strconv"
 
11
        "time"
 
12
 
 
13
        "github.com/ajstarks/svgo"
 
14
)
 
15
 
 
16
var canvas = svg.New(os.Stdout)
 
17
 
 
18
func main() {
 
19
        width := 512
 
20
        height := 256
 
21
        var n = 256
 
22
        var rx, ry int
 
23
 
 
24
        if len(os.Args) > 1 {
 
25
                n, _ = strconv.Atoi(os.Args[1])
 
26
        }
 
27
 
 
28
        f, _ := os.Open("/dev/urandom")
 
29
        x := make([]byte, n)
 
30
        y := make([]byte, n)
 
31
        f.Read(x)
 
32
        f.Read(y)
 
33
        f.Close()
 
34
 
 
35
        rand.Seed(int64(time.Now().Nanosecond()) % 1e9)
 
36
        canvas.Start(600, 400)
 
37
        canvas.Title("Random Integer Comparison")
 
38
        canvas.Desc("Comparison of Random integers: the random device & the Go rand package")
 
39
        canvas.Rect(0, 0, width/2, height, "fill:white; stroke:gray")
 
40
        canvas.Rect(width/2, 0, width/2, height, "fill:white; stroke:gray")
 
41
 
 
42
        canvas.Desc("Left: Go rand package (red), Right: /dev/urandom")
 
43
        canvas.Gstyle("stroke:none; fill-opacity:0.5")
 
44
        for i := 0; i < n; i++ {
 
45
                rx = rand.Intn(255)
 
46
                ry = rand.Intn(255)
 
47
                canvas.Circle(rx, ry, 5, canvas.RGB(127, 0, 0))
 
48
                canvas.Circle(int(x[i])+255, int(y[i]), 5, "fill:black")
 
49
        }
 
50
        canvas.Gend()
 
51
 
 
52
        canvas.Desc("Legends")
 
53
        canvas.Gstyle("text-anchor:middle; font-size:18; font-family:Calibri")
 
54
        canvas.Text(128, 280, "Go rand package", "")
 
55
        canvas.Text(384, 280, "/dev/urandom")
 
56
        canvas.Text(256, 280, fmt.Sprintf("n=%d", n), "font-size:12")
 
57
        canvas.Gend()
 
58
        canvas.End()
 
59
}