github.com/gopherjs/gopherjs@v1.19.0-beta1.0.20240506212314-27071a8796e4/compiler/natives/src/runtime/fastrand.go (about)

     1  //go:build js
     2  // +build js
     3  
     4  package runtime
     5  
     6  import "github.com/gopherjs/gopherjs/js"
     7  
     8  func fastrand() uint32 {
     9  	// In the upstream this function is implemented with a
    10  	// custom algorithm that uses bit manipulation, but it is likely to be slower
    11  	// than calling Math.random().
    12  	// TODO(nevkontakte): We should verify that it actually is faster and has a
    13  	// similar distribution.
    14  	return uint32(js.Global.Get("Math").Call("random").Float() * (1<<32 - 1))
    15  }
    16  
    17  func fastrandn(n uint32) uint32 {
    18  	return fastrand() % n
    19  }
    20  
    21  func fastrand64() uint64 {
    22  	return uint64(fastrand())<<32 | uint64(fastrand())
    23  }
    24  
    25  func fastrandu() uint {
    26  	return uint(fastrand())
    27  }