-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrandom.go
301 lines (255 loc) · 7.15 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
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
package gaul
import (
"math/rand"
"time"
"github.com/ojrac/opensimplex-go"
"github.com/peterhellberg/gfx"
)
const (
defaultScale = 0.001
defaultOctaves = 1
defaultPersistence = 0.9
defaultLacunarity = 2.0
)
// Rng is a random number generator with a system PRNG and simplex noise
type Rng struct {
seed int64
Prng LFSRLarge
Noise opensimplex.Noise
Simplex *gfx.SimplexNoise
octaves int
persistence float64
lacunarity float64
xscale float64
yscale float64
zscale float64
wscale float64
xoffset float64
yoffset float64
zoffset float64
woffset float64
}
// NewRng returns a PRNG with a system and Noise generator
func NewRng(i int64) Rng {
return Rng{
seed: i,
Prng: NewLFSRLargeWithSeed(uint64(i)),
Noise: opensimplex.New(i),
Simplex: gfx.NewSimplexNoise(i),
octaves: defaultOctaves,
persistence: defaultPersistence,
lacunarity: defaultLacunarity,
xscale: defaultScale,
yscale: defaultScale,
zscale: defaultScale,
xoffset: 0,
yoffset: 0,
zoffset: 0,
}
}
func (r *Rng) SetSeed(seed int64) {
r.seed = seed
r.Prng = NewLFSRLargeWithSeed(uint64(seed))
r.Prng.Next()
r.Noise = opensimplex.NewNormalized(seed)
r.Simplex = gfx.NewSimplexNoise(seed)
}
func (r *Rng) Gaussian(mean float64, stdev float64) float64 {
return rand.NormFloat64()*stdev + mean
}
// The Noise scale functions scale the position values passed into the
// Noise PRNG. Typically for screen coordinates scale values in the
// range of 0.001 to 0.01 produce visually appealing Noise
// SetNoiseScaleX scales the x position in Noise calculations
func (r *Rng) SetNoiseScaleX(scale float64) {
r.xscale = scale
}
// SetNoiseScaleY scales the y position in Noise calculations
func (r *Rng) SetNoiseScaleY(scale float64) {
r.yscale = scale
}
// SetNoiseScaleZ scales the z position in Noise calculations
func (r *Rng) SetNoiseScaleZ(scale float64) {
r.zscale = scale
}
// The Noise offset functions simple increment/decrement the
// position values before scaling
// SetNoiseOffsetX offsets the x position in Noise calculations
func (r *Rng) SetNoiseOffsetX(offset float64) {
r.xoffset = offset
}
// SetNoiseOffsetY offsets the y position in Noise calculations
func (r *Rng) SetNoiseOffsetY(offset float64) {
r.yoffset = offset
}
// SetNoiseOffsetZ offsets the z position in Noise calculations
func (r *Rng) SetNoiseOffsetZ(offset float64) {
r.zoffset = offset
}
// SetNoiseOctaves sets the number of steps when calculating fractal Noise
func (r *Rng) SetNoiseOctaves(i int) {
r.octaves = i
}
// SetNoisePersistence sets how amplitude scales with octaves
func (r *Rng) SetNoisePersistence(p float64) {
r.persistence = p
}
// SetNoiseLacunarity sets how frequency scales with octaves
func (r *Rng) SetNoiseLacunarity(l float64) {
r.lacunarity = l
}
// SignedNoise1D generates 1D Noise values in the range of [-1, 1]
func (r *Rng) SignedNoise1D(x float64) float64 {
return Map(0, 1, -1, 1, r.calcNoise1(x))
}
// SignedNoise2D generates 2D Noise values in the range of [-1, 1]
func (r *Rng) SignedNoise2D(x float64, y float64) float64 {
return Map(0, 1, -1, 1, r.calcNoise2(x, y))
}
// SignedNoise3D generates 3D Noise values in the range of [-1, 1]
func (r *Rng) SignedNoise3D(x float64, y float64, z float64) float64 {
return Map(0, 1, -1, -1, r.calcNoise3(x, y, z))
}
// Noise1D 1D Noise values in the range of [0, 1]
func (r *Rng) Noise1D(x float64) float64 {
return r.calcNoise1(x)
}
// Noise2D generates 2D Noise values in the range of [0, 1]
func (r *Rng) Noise2D(x float64, y float64) float64 {
return r.calcNoise2(x, y)
}
// Noise3D generates 3D Noise values in the range of [0, 1]
func (r *Rng) Noise3D(x float64, y float64, z float64) float64 {
return r.calcNoise3(x, y, z)
}
// UniformRandomPoints generates a list of points whose coordinates
// follow a uniform random distribution within a rectangle
func (r *Rng) UniformRandomPoints(num int, rect Rect) []Point {
points := make([]Point, num)
for i := 0; i < num; i++ {
x := rect.X + rand.Float64()*rect.W
y := rect.Y + rand.Float64()*rect.H
points[i] = Point{X: x, Y: y}
}
return points
}
func (r *Rng) NoisyRandomPoints(num int, threshold float64, rect Rect) []Point {
var points []Point
maxtries := 10 * num
i := 0
for len(points) < num && i < maxtries {
x := rect.X + rand.Float64()*rect.W
y := rect.Y + rand.Float64()*rect.H
noise := r.Noise2D(x, y)
if noise >= threshold {
points = append(points, Point{X: x, Y: y})
}
i++
}
return points
}
func (r *Rng) calcNoise1(x float64) float64 {
return r.calcNoise(1, x, 0, 0, 0)
}
func (r *Rng) calcNoise2(x, y float64) float64 {
return r.calcNoise(2, x, y, 0, 0)
}
func (r *Rng) calcNoise3(x, y, z float64) float64 {
return r.calcNoise(3, x, y, z, 0)
}
func (r *Rng) calcNoise4(x, y, z, w float64) float64 {
return r.calcNoise(4, x, y, z, w)
}
func (r *Rng) calcNoise(dim int, x, y, z, w float64) float64 {
totalNoise := 0.0
totalAmp := 0.0
amp := 1.0
freq := 1.0
for i := 0; i < r.octaves; i++ {
switch dim {
case 1:
totalNoise += r.Simplex.Noise2D(
(x+r.xoffset)*r.xscale*freq,
0,
)
case 2:
totalNoise += r.Simplex.Noise2D(
(x+r.xoffset)*r.xscale*freq,
(y+r.yoffset)*r.yscale*freq,
)
case 3:
totalNoise += r.Simplex.Noise3D(
(x+r.xoffset)*r.xscale*freq,
(y+r.yoffset)*r.yscale*freq,
(z+r.zoffset)*r.zscale*freq,
)
case 4:
totalNoise += r.Simplex.Noise4D(
(x+r.xoffset)*r.xscale*freq,
(y+r.yoffset)*r.yscale*freq,
(z+r.zoffset)*r.zscale*freq,
(w+r.zoffset)*r.wscale*freq,
)
}
totalAmp += amp
amp *= r.persistence
freq *= r.lacunarity
}
return totalNoise / totalAmp
}
type LFSRSmall struct {
state uint16
}
func NewLFSRSmallWithSeed(seed uint16) LFSRSmall {
return LFSRSmall{state: seed}
}
func NewLFSRSmall() LFSRSmall {
return LFSRSmall{state: uint16(time.Now().UnixNano())}
}
func (l *LFSRSmall) Next() uint16 {
b := ((l.state >> 0) ^ (l.state >> 2) ^ (l.state >> 3) ^ (l.state >> 5)) & 1
l.state = (l.state >> 1) | (b << 15)
return l.state
}
type LFSRMedium struct {
state uint32
}
func NewLFSRMediumWithSeed(seed uint32) LFSRMedium {
return LFSRMedium{state: seed}
}
func NewLFSRMedium() LFSRMedium {
return LFSRMedium{state: uint32(time.Now().UnixNano())}
}
func (l *LFSRMedium) Next() uint32 {
l.state ^= l.state << 13
l.state ^= l.state >> 17
l.state ^= l.state << 5
return l.state
}
// LFSRLarge is a 64-bit Xorshift PRNG
// Benchmarks show this is faster than LFSRMedium and LFSRSmall, so you might as well use this one.
// Benchmarks also show this is 6-7x faster than the standard math/rand PRNG
type LFSRLarge struct {
state uint64
}
func NewLFSRLargeWithSeed(seed uint64) LFSRLarge {
return LFSRLarge{state: seed}
}
func NewLFSRLarge() LFSRLarge {
return LFSRLarge{state: uint64(time.Now().UnixNano())}
}
func (l *LFSRLarge) Next() uint64 {
l.state ^= l.state << 13
l.state ^= l.state >> 7
l.state ^= l.state << 17
return l.state
}
func (l *LFSRLarge) Float64() float64 {
return float64(l.Next()) / float64(^uint64(0))
}
func (l *LFSRLarge) Uint64n(n uint64) uint64 {
return l.Next() % n
}
func (l *LFSRLarge) Uint64() uint64 {
return l.Next()
}