Adds rand-seed procedure, which will set the seed value for random number generation

This commit is contained in:
sloum 2022-06-08 14:26:30 -07:00
parent 860a1359a1
commit 416b4fa778
4 changed files with 19 additions and 4 deletions

View File

@ -189,7 +189,7 @@ Quote and list both have some syntactic sugar to create a shorthand for their us
Implemented:
`positive?`, `negative?`, `zero?`, `abs`, `floor`, `ceil`, `round`, `+`, `-`, `*`, `/`, `min`, `max`, `number->string`, `rune->string`, `rand`, `%`, `cos`, `sin`, `tan`, `sqrt`, `atan`, `sqrt`
`positive?`, `negative?`, `zero?`, `abs`, `floor`, `ceil`, `round`, `+`, `-`, `*`, `/`, `min`, `max`, `number->string`, `rune->string`, `rand`, `rand-seed`, `%`, `cos`, `sin`, `tan`, `sqrt`, `atan`, `sqrt`
<details>
@ -218,7 +218,8 @@ Implemented:
<li><code>(max [number...])</code>: <code>number</code></li>
<li><code>(number->string [number] [[base: number]])</code>: <code>string</code></li>
<li><code>(rune->string [number])</code>: <code>string</code></li>
<li><code>(rand [[max]] [[min]])</code>: <code>number</code>
<li><code>(rand [[max: number]] [[min: number]])</code>: <code>number</code>
<li><code>(rand-seed [number])</code>: <code>#t</code>
</ul>
</details>

12
lib.go
View File

@ -2618,8 +2618,18 @@ var stdLibrary = vars{
}
return make([]expression, 0)
},
"rand-seed": func(a ...expression) expression {
if len(a) == 0 {
return exception("'rand-seed' expected a number, no value was given")
}
seed, ok := a[0].(number)
if !ok {
return exception("'rand-seed' expected a number, a non-number value was given")
}
rand.Seed(int64(seed))
return true
},
"rand": func(a ...expression) expression {
rand.Seed(time.Now().UnixNano())
if len(a) == 0 {
// equal to: (rand 1 0)
return number(rand.Float64())

View File

@ -4,6 +4,7 @@ import (
"flag"
"fmt"
"io/ioutil"
"math/rand"
"os"
"os/exec"
"os/signal"
@ -12,11 +13,12 @@ import (
"sort"
"strings"
"syscall"
"time"
ln "github.com/peterh/liner"
)
const version = "0.7.4"
const version = "0.7.5"
const globalLibPath = "/usr/local/lib/slope/modules/"
@ -85,6 +87,7 @@ func Init() {
c := make(chan os.Signal)
signal.Notify(c, syscall.SIGINT)
go handleSignals(c)
rand.Seed(time.Now().UnixNano())
}
func outputResult(txt string) {

View File

@ -129,6 +129,7 @@ var usageStrings = map[string]string{
"pwd": "(pwd) => string\n\nReturns the current working directory as a string",
"quote": "(quote [expression...]) => bool",
"rand": "(rand [[max: number]] [[min: number]]) => number\n\nIf no arguments are given `rand` will produce a floating point number between 0 and 1. If only `max` is given, `rand` will produce a floating point number between 0 and `max`. If `max` and `min` are both given, `rand` will produce a floating point number between `min` and `max`. `max` is always exclusive (the value produced will always be less than `max`), while `min` is inclusize. To obtain integers use `floor`, `ceil`, or `round` on the result or `rand`",
"rand-seed": "(rand-seed [number]) => #t\n\nSets the seed value for generating random numbers. The seed value defaults to the timestamp that is acquired when slope starts up. This procedure will over-ride that behavior to use the given seed",
"range": "(range [[count: number]] [[start: number]] [[step: number]]) => list\n\nIf no arguments are given an empty list will be returned. `count` should be a positive integer and represents the number of list items that will be produced by `range`. If no arguments other than `count` are given then a list from 0 -> count will be produced. To start at a number other than zero `start` can be given. To step by a value other than one, `step` can be given.\n\n\t(range) => ()\n\t(range 5) => (0 1 2 3 4)\n\t(range 5 10) => (10 11 12 13 14)\n\t(range 5 2 2) => (2 4 6 8 10)",
"read-all": "(read-all [[IOHandle]]) => string\n\nReads all content from the given io-handle",
"read-all-lines": "(read-all-lines [[IOHandle]]) => list\n\nReads all content from the given io-handle and splits it into a list on newline",