gitee.com/quant1x/num@v0.3.2/internal/functions/random.go (about)

     1  package functions
     2  
     3  import (
     4  	"gitee.com/quant1x/num/internal/constraints"
     5  	"gitee.com/quant1x/num/internal/rand"
     6  )
     7  
     8  func Random[T constraints.Integer | constraints.Float](size int) []T {
     9  	x := make([]T, size)
    10  	for i := 0; i < size; i++ {
    11  		x[i] = T(rand.NormFloat64() * 100)
    12  	}
    13  	return x
    14  }
    15  
    16  func RandomBool(size int, pctTrue float64) []bool {
    17  	res := make([]bool, size)
    18  	for i := 0; i < size; i++ {
    19  		res[i] = rand.Float64() < pctTrue
    20  	}
    21  	return res
    22  }
    23  
    24  func RandomRange[T constraints.Integer | constraints.Float](a, b T, size int) []T {
    25  	x := make([]T, size)
    26  	r := b - a
    27  	for i := 0; i < size; i++ {
    28  		x[i] = T(rand.Float64()*float64(r) + float64(a))
    29  	}
    30  	return x
    31  }