diff --git a/getting-started/random-library.md b/getting-started/random-library.md new file mode 100644 index 0000000..b5a13cb --- /dev/null +++ b/getting-started/random-library.md @@ -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. + diff --git a/getting-started/standard-library.md b/getting-started/standard-library.md index 676c30d..68d3ef2 100644 --- a/getting-started/standard-library.md +++ b/getting-started/standard-library.md @@ -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. diff --git a/index.md b/index.md index 7ce6aac..ca5b7dd 100644 --- a/index.md +++ b/index.md @@ -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)