/
random.go
125 lines (103 loc) · 2.52 KB
/
random.go
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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
package main
import (
"fmt"
"os"
"github.com/veandco/go-sdl2/sdl"
. "github.com/kevincolyer/GameEngine/GameEngine"
)
func wrapScreen(x, y float64) (float64, float64) {
return Wrap(x, 0, blocksw), Wrap(y, 0, blocksh)
}
var err error
func main() {
var ctx = New(2, 640, 320, "Random", nil)
onCreate(ctx)
var running = true
for running {
running = onUpdate(ctx, ctx.Elapsed())
}
ctx.Destroy()
os.Exit(0)
}
var blocksw, blocksh, blocks float64
var rand *gnuRand
var lastRandSeed uint32 = 0
var BLACK = Colour{0, 0, 0, 255}
var WHITE = Colour{255, 255, 255, 225}
func onCreate(c *Context) {
blocks = c.Blocks
blocksw = c.ScrnWidth
blocksh = c.ScrnHeight
c.Clear()
c.Present()
rand = NewgnuRand(0)
lastRandSeed = rand.Seed(0)
}
func onUpdate(c *Context, elapsed float64) (running bool) {
running, keys := c.PollQuitandKeys()
if keys.Event {
if keys.Key == "q" {
running = false
}
if keys.Key == " " && keys.Released {
lastRandSeed = rand.Seed(rand.Rnd())
println(lastRandSeed)
}
fmt.Println("pressed ", keys.Key)
}
c.Clear()
lastRandSeed = rand.Seed(lastRandSeed)
for x := 0.0; x < blocksw; x++ {
for y := 0.0; y < blocksh; y++ {
star := rand.Rand() < 0.15
if star {
c.SetDrawColor(WHITE)
} else {
c.SetDrawColor(BLACK)
}
c.Point(x, y)
}
}
mx, my, state := sdl.GetMouseState()
if state&sdl.ButtonLMask() == 1 {
c.SetDrawColor(Colour{255, 0, 0, 255})
c.DrawFillCircle(float64(mx)/blocks, float64(my)/blocks, 13.0)
}
c.SetDrawColor(Colour{255, 255, 0, 255})
c.DrawCircle(float64(mx)/blocks, float64(my)/blocks, 15.0)
c.Present()
Delay(10)
return running
}
// gnuRand object pseudo random number generator - good for 32bits
type gnuRand struct {
seed uint32
Size float32
}
// NewgnuRand constructor - pseudo random number generator - good for 32bits
func NewgnuRand(s uint32) *gnuRand {
return &gnuRand{seed: uint32(s), Size: 0x7FFFFFFF}
}
// gnuRand ... Seeds
func (g *gnuRand) Seed(s uint32) uint32 {
g.seed = s
return g.seed
}
// Rnd integer fullrange 0 -> upper bound
func (g *gnuRand) Rnd() uint32 {
g.seed = g.seed*1103515245 + 12345
return g.seed
//return float32(gnuRandSeed) / float32(0x7FFFFFFF)
}
// Rand float between 0 and 1
func (g *gnuRand) Rand() float32 {
return float32(g.Rnd()) / g.Size
}
// Rand float between min and max (signed)
func (g *gnuRand) RandF(min, max float32) float32 {
return g.Rand()/(max-min) + min
}
// Rand integer between min and max (signed)
func (g *gnuRand) RandI(min, max int32) int32 {
return int32(g.Rnd())%(max-min) + min
}