Consistent documentation across RNG

This commit is contained in:
MindfulMinun committed 2022-07-16 20:15:37 -05:00
1 parent 7d0db4d357
commit c9c9978a6b
1 file changed
+34 -11
+34 -11
View File
@@ -12,36 +12,59 @@ import { swap } from './iterable.ts'
*/
export abstract class BaseRNG implements Iterable<number> {
/**
* This is the main number generation function.
* It should return a number between 0 (inclusive) and 1 (exclusive) as well as update its internal state.
* This is the main number generation function. It should return a number
* between 0 (inclusive) and 1 (exclusive) as well as update its internal state.
* @author MindfulMinun
* @since 2022-07-16
*/
abstract nextValue(): number
/** nyanyanya */
/**
* Yields numbers between zero and one
* @author MindfulMinun
* @since 2022-07-16
*/
*[Symbol.iterator]() {
while (true) yield this.nextValue()
while (true) yield this.float()
}
/** Returns a floating point number between 0 (inclusive) and 1 (exclusive) */
/**
* Returns a floating point number between 0 (inclusive) and 1 (exclusive),
* using the algorithm implemented in {@link nextValue()}
* @author MindfulMinun
* @since 2022-07-16
*/
float() {
return this.nextValue()
}
/** Returns an integer between the provided bounds. This returns integers *up to* `upper`, without including it. */
/**
* Returns an integer between the provided bounds. This returns integers *up to* `upper`, without including it.
* @author MindfulMinun
* @since 2022-07-16
*/
int(upper: number): number
int(lower: number, upper: number): number
int(a: number, b?: number): number {
const rand = this.float()
const [min, max] = typeof b === "undefined" ? [0, a] : [a, b]
return Math.floor(rand * (max - min + 1) + min)
return Math.floor(this.float() * (max - min + 1) + min)
}
/** Chooses a random element from a provided array */
/**
* Chooses a random element from a provided array
* @author MindfulMinun
* @since 2022-07-16
*/
choose<T>(arr: T[]) {
return arr[Math.floor(this.float() * arr.length)]
}
/** Shuffles an array */
/**
* Shuffles an array. Note that this function swaps the elements
* *in place*, meaning the original array is modified.
* @author MindfulMinun
* @since 2022-07-16
*/
shuffle<T>(arr: T[]) {
let i, j
for (i = arr.length - 1; i > 0; i--) {
@@ -85,7 +108,7 @@ export abstract class BaseRNG implements Iterable<number> {
}
/**
* A simple RNG which returns numbers generated by {@link https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Math/random Math.random}
* A simple RNG which returns numbers generated by {@link https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Math/random Math.random()}
* @author MindfulMinun
* @since 2022-07-08
*/