github.com/tinygo-org/tinygo@v0.31.3-0.20240404173401-90b0bf646c27/testdata/rand.go (about)

     1  package main
     2  
     3  import "crypto/rand"
     4  
     5  // TODO: make this a test in the crypto/rand package.
     6  
     7  func main() {
     8  	buf := make([]byte, 500)
     9  	n, err := rand.Read(buf)
    10  	if n != len(buf) || err != nil {
    11  		println("could not read random numbers:", err)
    12  	}
    13  
    14  	// Very simple test that random numbers are at least somewhat random.
    15  	sum := 0
    16  	for _, b := range buf {
    17  		sum += int(b)
    18  	}
    19  	if sum < 95*len(buf) || sum > 159*len(buf) {
    20  		println("random numbers don't seem that random, the average byte is", sum/len(buf))
    21  	} else {
    22  		println("random number check was successful")
    23  	}
    24  }