Class: RandomNumberCache

PD. RandomNumberCache

A fast cache of repeating random numbers.


new RandomNumberCache( [size] [, min] [, max])

Creates a new cache of repeating random numbers.

If no size is given, the cache will remain empty until the first call to the reset() method. Until then, calls to next() will always return zero.

In terms of size, it is usually best to use a prime number to avoid situations where the repeating pattern becomes visible. The following are some prime examples: 61, 67, 71, 73, 79, 83, 89, 97, 101, 103, 107, 109, 113, 127, 131, 137, 139, 149, 151, 157, 163, 167, 173, 179, 181, 191, 193, 197, 199, 211, 223, 227, 229, 233, 239, 241, 251, 257, 263, 269, 271, 277, 281, 283 and 293.

Parameters:
Name Type Argument Description
size number <optional>

The number of entries in the cache, defaults to 128.

min number <optional>

The minimum value of the random number range, defaults to 0.

max number <optional>

The maximum value of the random number range, defaults to 1.

Author:
  • drajmarsh

Methods


getCacheSize()

Retrieves the current size of the cache.

Returns:

Returns the number of items in the cache.

Type
number

initialise( [size] [, min] [, max])

Rebuilds the cache with new numbers based on the given size and range.

If no range is given, or either value is not a valid number, the range defaults to fractional values between 0 and 1.

In terms of size, it is usually best to use a prime number to avoid situations where the repeating pattern becomes visible. The following are some prime examples: 61, 67, 71, 73, 79, 83, 89, 97, 101, 103, 107, 109, 113, 127, 131, 137, 139, 149, 151, 157, 163, 167, 173, 179, 181, 191, 193, 197, 199, 211, 223, 227, 229, 233, 239, 241, 251, 257, 263, 269, 271, 277, 281, 283 and 293.

NOTE: To prevent inadvertent memory use, the size of this cache is limited to between 2 and 16384 items.

Parameters:
Name Type Argument Description
size number <optional>

The number of entries in the cache, defaults to 127.

min number <optional>

The minimum value of the random number range, defaults to 0.

max number <optional>

The maximum value of the random number range, defaults to 1.

Returns:

Returns this cache instance to support method chaining.

Type
PF.RandomNumberCache

next()

Retrieve the next item in the random number cache.

Each access increments the cache index which will then wrap back once it reaches the end.

Returns:

Returns a the next number from the cache.

Type
number

reset()

Sets the index back to zero ready to start repeating.

Returns:

Returns this cache instance to support method chaining.

Type
PD.RandomNumberCache