mirror of
https://github.com/MontFerret/ferret.git
synced 2025-03-17 21:18:37 +02:00
* Added release notes * #509 fixedOCOD typo * Updated values * Updated comments * Changed stdlib docs format * Changed format of array in docs * Use 'any' instead of 'value' in docs * New format for optional params * Updated docs for testing package * Added namespace information
41 lines
874 B
Go
41 lines
874 B
Go
package math
|
|
|
|
import (
|
|
"context"
|
|
"math/rand"
|
|
"time"
|
|
|
|
"github.com/MontFerret/ferret/pkg/runtime/core"
|
|
"github.com/MontFerret/ferret/pkg/runtime/values"
|
|
)
|
|
|
|
// RAND return a pseudo-random number between 0 and 1.
|
|
// @param {Int | Float} [max] - Upper limit.
|
|
// @param {Int | Float} [min] - Lower limit.
|
|
// @return {Float} - A number greater than 0 and less than 1.
|
|
func Rand(_ context.Context, args ...core.Value) (core.Value, error) {
|
|
err := core.ValidateArgs(args, 0, 2)
|
|
|
|
if err != nil {
|
|
return values.None, err
|
|
}
|
|
|
|
if len(args) == 0 {
|
|
rand.Seed(time.Now().UnixNano())
|
|
return values.NewFloat(rand.Float64()), nil
|
|
}
|
|
|
|
var max float64
|
|
var min float64
|
|
|
|
max = float64(values.ToFloat(args[0]))
|
|
|
|
if len(args) > 1 {
|
|
min = float64(values.ToFloat(args[1]))
|
|
} else {
|
|
max, min = core.NumberBoundaries(max)
|
|
}
|
|
|
|
return values.NewFloat(core.Random(max, min)), nil
|
|
}
|