aboutsummaryrefslogtreecommitdiffstats
path: root/jsre/jsre.go
diff options
context:
space:
mode:
authorFelix Lange <fjl@twurst.com>2016-02-19 20:17:10 +0800
committerFelix Lange <fjl@twurst.com>2016-02-19 20:17:10 +0800
commitc305005d831eccf9d65c7b55f817390d2334e666 (patch)
treec15ee2d32fca381b8fcd45ad7d761a3d93b367a3 /jsre/jsre.go
parent17649edd85b98f00f781415ff02da8441e52db99 (diff)
parent6777531a2d3367ef3cee933d7a36945eecb37090 (diff)
downloadgo-tangerine-c305005d831eccf9d65c7b55f817390d2334e666.tar
go-tangerine-c305005d831eccf9d65c7b55f817390d2334e666.tar.gz
go-tangerine-c305005d831eccf9d65c7b55f817390d2334e666.tar.bz2
go-tangerine-c305005d831eccf9d65c7b55f817390d2334e666.tar.lz
go-tangerine-c305005d831eccf9d65c7b55f817390d2334e666.tar.xz
go-tangerine-c305005d831eccf9d65c7b55f817390d2334e666.tar.zst
go-tangerine-c305005d831eccf9d65c7b55f817390d2334e666.zip
Merge pull request #2227 from bas-vk/mathrandom
console: seed random number generator
Diffstat (limited to 'jsre/jsre.go')
-rw-r--r--jsre/jsre.go18
1 files changed, 18 insertions, 0 deletions
diff --git a/jsre/jsre.go b/jsre/jsre.go
index a4c9d970b..f4464910d 100644
--- a/jsre/jsre.go
+++ b/jsre/jsre.go
@@ -18,8 +18,11 @@
package jsre
import (
+ crand "crypto/rand"
+ "encoding/binary"
"fmt"
"io/ioutil"
+ "math/rand"
"sync"
"time"
@@ -70,6 +73,18 @@ func New(assetPath string) *JSRE {
return re
}
+// randomSource returns a pseudo random value generator.
+func randomSource() *rand.Rand {
+ bytes := make([]byte, 8)
+ seed := time.Now().UnixNano()
+ if _, err := crand.Read(bytes); err == nil {
+ seed = int64(binary.LittleEndian.Uint64(bytes))
+ }
+
+ src := rand.NewSource(seed)
+ return rand.New(src)
+}
+
// This function runs the main event loop from a goroutine that is started
// when JSRE is created. Use Stop() before exiting to properly stop it.
// The event loop processes vm access requests from the evalQueue in a
@@ -81,6 +96,9 @@ func New(assetPath string) *JSRE {
// called from JS through an RPC call.
func (self *JSRE) runEventLoop() {
vm := otto.New()
+ r := randomSource()
+ vm.SetRandomSource(r.Float64)
+
registry := map[*jsTimer]*jsTimer{}
ready := make(chan *jsTimer)