Added random library

This commit is contained in:
2023-02-23 19:39:34 +11:00
committed by GitHub
parent 2c594f726d
commit 1ac49def37
3 changed files with 53 additions and 4 deletions

View File

@@ -0,0 +1,31 @@
# Random Library
The random library offers a number of functions geared towards producing pseudorandom values. This library has a concept called "generators", which are opaque objects used to generate a sequence of numbers from an initial integer seed. A seed can be generated from most values using the standard library `hash` function.
The random library can usually be accessed with the `import` keyword:
```
import standard;
import random;
var generator: opaque = createRandomGenerator(clock().hash());
```
The current implementation is minimal in nature, and will be expanded or replaced in future.
## createRandomGenerator(seed: int)
This function creates a new generator opaque based on the given seed. The same seed will produce the same sequence of pseudorandom outputs from different generators using `generateRandomNumber`.
Every generator must also be freed with `freeRandomGenerator`.
## generateRandomNumber(self: opaque)
This function takes in a generator opaque, and returns a pseudorandom integer value.
This function also mutates the generator's internal state.
## freeRandomGenerator(self: opaque)
This function frees an existing generator opaque.

View File

@@ -8,6 +8,10 @@ The standard library can usually be accessed with the `import` keyword:
import standard;
```
## abs(self)
This function returns the absolute value of any integer or float passed in.
## clock()
This function returns a string representation of the current timestamp.
@@ -50,11 +54,24 @@ This function takes either an array or a dictionary as the `self` argument, and
## getKeys(self: dictionary)
This returns an array of all non-null keys stored within the dictionary. The order is undefined.
This function returns an array of all non-null keys stored within the dictionary. The order is undefined.
## getValues(self: dictionary)
This returns an array of all values with non-null keys stored within the dictionary. The order is undefined.
This function returns an array of all values with non-null keys stored within the dictionary. The order is undefined.
## hash(self)
This function returns a hashed value of `self`.
This function uses the internal literal hashing algorithms. As such, the following can't be hashed:
* functions
* types
* opaques
* `null`
Any attempt to hash these will return -1, except `null` which returns 0.
## indexOf(self: array, value)
@@ -139,9 +156,9 @@ These characters used because they are the only control characters currently sup
## trimBegin(self: string, trimChars: string = " \t\n\r")
This is identical to `_trim(self, trimChars)`, except it is only applied to the beginning of the first argument.
This function is identical to `trim(self, trimChars)`, except it is only applied to the beginning of the first argument.
## trimEnd(self: string, trimChars: string = " \t\n\r")
This is identical to `_trim(self, trimChars)`, except it is only applied to the end of the first argument.
This function is identical to `trim(self, trimChars)`, except it is only applied to the end of the first argument.

View File

@@ -49,6 +49,7 @@ print tally(); //3
* [Types](getting-started/types)
* [About Library](getting-started/about-library)
* [Standard Library](getting-started/standard-library)
* [Random Library](getting-started/random-library)
* [Runner Library](getting-started/runner-library)
* [Game Engine](getting-started/game-engine)