random

Generates a decimal random value in the half-open range [0, 1).

Summary

Use this when random decimal values should stay in Arith form. Set CRYPTO: true only when the runtime provides crypto support.

AI Contract

FieldValue
Kindstatic method
Canonical namerandom
AliasesNone
Mutates receiverNo
ReturnsArithInstance
Accepts (string, base) overloadNo
Configuration dependenciesDECIMAL_PLACES, CRYPTO
Related methodsconfig

Signature

Arith.random(decimalPlaces?: number): ArithInstance;

Parameters

ParameterTypeRequiredNotes
decimalPlacesnumberNoMaximum decimal places. Defaults to current DECIMAL_PLACES.

Returns

Returns a new Arith instance r where 0 <= r < 1.

Behavior

  • Uses decimalPlaces if provided, otherwise current DECIMAL_PLACES.
  • Produced values have at most the requested number of decimal places.
  • When CRYPTO is true, uses globalThis.crypto.getRandomValues or compatible random bytes support.
  • When CRYPTO is false, uses Math.random.

Examples

import { Arith } from "@teakit/arith";

const r = Arith.random(8);

r.gte("0"); // true
r.lt("1"); // true
r.decimalPlaces() <= 8; // true

Errors

  • Throws if decimalPlaces is invalid.
  • Throws if CRYPTO is true but no crypto source is available.

Agent Notes

  • Do not assume deterministic output in tests; assert range and decimal-place constraints instead.
  • Import from @teakit/arith; prefer import { Arith } from "@teakit/arith".
  • Static helpers are called as Arith.method(...) and do not require an instance receiver.
  • Do not generate BigNumber, Decimal, isBigNumber, or isDecimal compatibility APIs.
  • Use string inputs for exact decimal values when a static helper accepts numeric values.

See Also