Skip to content
This repository was archived by the owner on Apr 23, 2025. It is now read-only.

Commit 81a476a

Browse files
committed
add Random and RandomSeed
1 parent d07f3b4 commit 81a476a

File tree

5 files changed

+62
-0
lines changed

5 files changed

+62
-0
lines changed

api.go

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -89,3 +89,15 @@ func Shear(x, y float64) {
8989
func Matrix(a, b, c, d, e, f float64) {
9090
gproc.Matrix(a, b, c, d, e, f)
9191
}
92+
93+
// RandomSeed changes the sequence of numbers generated by Random.
94+
func RandomSeed(seed int64) {
95+
gproc.RandomSeed(seed)
96+
}
97+
98+
// Random returns, as a float64, a pseudo-random number in [min,max).
99+
// Random will produce a different sequence of number every time the program runs.
100+
// To make the generated numbers deterministic, call RandomSeed with a specific seed.
101+
func Random(min, max float64) float64 {
102+
return gproc.Random(min, max)
103+
}

api_test.go

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -90,3 +90,34 @@ func TestBezier(t *testing.T) {
9090
gproc = proc.Proc
9191
proc.Run(t)
9292
}
93+
94+
func TestRandom(t *testing.T) {
95+
old := gproc
96+
defer func() {
97+
gproc = old
98+
}()
99+
100+
// Use a specific seed to make tests deterministic
101+
RandomSeed(100)
102+
103+
tests := []struct {
104+
min float64
105+
max float64
106+
want float64
107+
}{
108+
{0, 5, 4.082513468898083},
109+
{-5, -0, -0.19869552746956476},
110+
{1, 4, 1.181324427195803},
111+
{-4, -1, -3.0641855722578617},
112+
{0, 0, 0},
113+
{-1, -1, -1},
114+
{1, 1, 1},
115+
}
116+
for _, tt := range tests {
117+
t.Run("", func(t *testing.T) {
118+
if got := Random(tt.min, tt.max); got != tt.want {
119+
t.Errorf("Random() = %v, want %v", got, tt.want)
120+
}
121+
})
122+
}
123+
}

proc.go

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ import (
1414
"io"
1515
"log"
1616
"math"
17+
"math/rand"
1718
"os"
1819
"path/filepath"
1920
"strings"
@@ -99,6 +100,12 @@ func newProc(w, h int) *Proc {
99100
proc.cfg.th = material.NewTheme(gofont.Collection())
100101
proc.stk.cur().stroke.style.Width = 2
101102

103+
// If Seed is not called, the generator will produce the same sequence of number every time the
104+
// program runs, since the default seed is 1 (see rand.Seed).
105+
// Setting a different seed everytime the program starts will make the behavior of Random()
106+
// similar to Processing/p5.js.
107+
rand.Seed(time.Now().UTC().UnixNano())
108+
102109
return proc
103110
}
104111

@@ -378,3 +385,15 @@ func (p *Proc) Screenshot(fname string) error {
378385

379386
return nil
380387
}
388+
389+
// RandomSeed changes the sequence of numbers generated by Random.
390+
func (p *Proc) RandomSeed(seed int64) {
391+
rand.Seed(seed)
392+
}
393+
394+
// Random returns, as a float64, a pseudo-random number in [min,max).
395+
// Random will produce a different sequence of number every time the program runs.
396+
// To make the generated numbers deterministic, call RandomSeed with a specific seed.
397+
func (p *Proc) Random(min, max float64) float64 {
398+
return rand.Float64()*(max-min) + min
399+
}

testdata/api_shapes.png

12.6 KB
Loading

testdata/api_shapes_bezier.png

8.17 KB
Loading

0 commit comments

Comments
 (0)