random

Generates an Arith decimal value in the half-open range [0, 1).

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

const r = Arith.random(8);

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

// Omit decimalPlaces to use the current DECIMAL_PLACES config.
const DefaultRandom = Arith.clone({ DECIMAL_PLACES: 3 });
const value = DefaultRandom.random();
value.ge("0"); // true
value.lt("1"); // true
value.decimalPlaces()! <= 3; // true

// random(0) produces an integer 0 in the half-open range [0, 1).
Arith.random(0).toString(); // "0"

API Reference

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.

Throws

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

Agent Contract

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

Agent Notes

  • Do not assume deterministic output in tests; assert range and decimal-place constraints instead.
  • Import with import { Arith } from "@teakit/arith"; default imports are unsupported.
  • 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.