{"version":3,"file":"_randomInteger.mjs","names":[],"sources":["../../src/internal/_randomInteger.ts"],"sourcesContent":["import { OpenApi } from \"@typia/interface\";\n\nimport { _randomMultiple } from \"./_randomMultiple\";\n\n/**\n * Generate a random integer from an integer schema.\n *\n * Bounds are tightened to the integers they contain, a missing side defaults to\n * a window of one hundred beside the other, and a step is satisfied through the\n * decimal multiple generator.\n *\n * The optional source supplies draws in [0, 1); it defaults to the platform\n * source resolved when this helper is called. Nested draws use the same\n * source.\n *\n * @evidence contracts/common.md#principled-implementation The bounds are tightened to the integers they contain, an exclusive bound moves to the next representable integer, a missing side defaults to a window of one hundred around the other, an infinite bound that excludes nothing is dropped and one that excludes everything throws; the scalar draw maps the source fraction with floor(source() * (maximum - minimum + 1)) + minimum when the width is finite, or weighted finite endpoints otherwise, and clamps rounding to the selected bounds, or selects a multiple through the decimal multiple generator. Uniform index probabilities on exactly representable integer intervals require a uniform source; intervals beyond exact integer precision can return only representable numbers. An injected deterministic source retains the same bound arithmetic.\n * @evidence contracts/common.md#clear-and-simple-design One function with private boundary helpers shared in shape with the number generator.\n * @evidence contracts/common.md#prohibited-implementation-shortcuts Errors are explicit, and the default window of one hundred is an arbitrary generator choice that is documented by its use only.\n * @evidence contracts/common.md#meaningful-documentation The doc states how the bounds, the missing side and the step are handled, and the boundary helper explains the infinite bound rule.\n */\nexport const _randomInteger = (\n  schema: OpenApi.IJsonSchema.IInteger,\n  source: () => number = Math.random,\n): number => {\n  const lower: IBoundary | null = getLowerBoundary(schema);\n  const upper: IBoundary | null = getUpperBoundary(schema);\n  const minimum: number =\n    lower?.value ?? (upper === null ? 0 : upper.value - 100);\n  const maximum: number =\n    upper?.value ?? (lower === null ? 100 : lower.value + 100);\n  if (minimum > maximum)\n    throw new Error(\"Minimum value is greater than maximum value.\");\n  return schema.multipleOf === undefined\n    ? scalar({ minimum, maximum }, source)\n    : _randomMultiple(\n        {\n          minimum,\n          maximum,\n          multipleOf: schema.multipleOf,\n          exclusiveMinimum: lower?.exclusive ?? false,\n          exclusiveMaximum: upper?.exclusive ?? false,\n          integer: true,\n        },\n        source,\n      );\n};\n\nconst scalar = (\n  props: { minimum: number; maximum: number },\n  source: () => number,\n): number => {\n  const minimum: number = Math.ceil(props.minimum);\n  const maximum: number = Math.floor(props.maximum);\n  if (minimum > maximum) throw new Error(\"The integer range is empty.\");\n  if (!Number.isFinite(minimum) || !Number.isFinite(maximum))\n    throw new Error(\"The integer range has no finite value.\");\n  const draw: number = source();\n  const width: number = maximum - minimum + 1;\n  const value: number = Number.isFinite(width)\n    ? Math.floor(draw * width) + minimum\n    : Math.floor((1 - draw) * minimum + draw * maximum);\n  return Math.max(minimum, Math.min(maximum, value));\n};\n\nconst getLowerBoundary = (\n  schema: OpenApi.IJsonSchema.IInteger,\n): IBoundary | null => {\n  const inclusive: IBoundary | null = boundary(schema.minimum, false, \"lower\");\n  const exclusive: IBoundary | null = boundary(\n    schema.exclusiveMinimum,\n    true,\n    \"lower\",\n  );\n  const selected: IBoundary | null = selectBoundary(\n    inclusive,\n    exclusive,\n    Math.max,\n  );\n  if (selected === null) return null;\n  const integer: number = selected.exclusive\n    ? Math.floor(selected.value) + 1\n    : Math.ceil(selected.value);\n  return {\n    value:\n      selected.exclusive && integer <= selected.value\n        ? nextRepresentable(selected.value, true)\n        : integer,\n    exclusive: false,\n  };\n};\n\nconst getUpperBoundary = (\n  schema: OpenApi.IJsonSchema.IInteger,\n): IBoundary | null => {\n  const inclusive: IBoundary | null = boundary(schema.maximum, false, \"upper\");\n  const exclusive: IBoundary | null = boundary(\n    schema.exclusiveMaximum,\n    true,\n    \"upper\",\n  );\n  const selected: IBoundary | null = selectBoundary(\n    inclusive,\n    exclusive,\n    Math.min,\n  );\n  if (selected === null) return null;\n  const integer: number = selected.exclusive\n    ? Math.ceil(selected.value) - 1\n    : Math.floor(selected.value);\n  return {\n    value:\n      selected.exclusive && integer >= selected.value\n        ? nextRepresentable(selected.value, false)\n        : integer,\n    exclusive: false,\n  };\n};\n\n/**\n * Reads one bound, which may be infinite.\n *\n * A type tag built from an overflowing literal (`Maximum<1e400>`) carries an\n * infinite bound (#2452). `-Infinity` below or `Infinity` above excludes no\n * finite number, so it is no bound at all; `Infinity` below or `-Infinity`\n * above excludes every finite number, so no value can be generated.\n */\nconst boundary = (\n  value: number | undefined,\n  exclusive: boolean,\n  side: \"lower\" | \"upper\",\n): IBoundary | null => {\n  if (value === undefined) return null;\n  if (Number.isFinite(value)) return { value, exclusive };\n  if (value === (side === \"lower\" ? -Infinity : Infinity)) return null;\n  throw new Error(\"Numeric range has no finite value.\");\n};\n\nconst selectBoundary = (\n  x: IBoundary | null,\n  y: IBoundary | null,\n  compare: (x: number, y: number) => number,\n): IBoundary | null => {\n  if (x === null) return y;\n  if (y === null) return x;\n  if (x.value === y.value)\n    return { value: x.value, exclusive: x.exclusive || y.exclusive };\n  return compare(x.value, y.value) === x.value ? x : y;\n};\n\ninterface IBoundary {\n  value: number;\n  exclusive: boolean;\n}\n\n/**\n * Advances one IEEE 754 binary64 bit pattern, including signed zero and\n * subnormals.\n */\nconst nextRepresentable = (value: number, upward: boolean): number => {\n  if (value === 0) return upward ? Number.MIN_VALUE : -Number.MIN_VALUE;\n  const view: DataView = new DataView(new ArrayBuffer(8));\n  view.setFloat64(0, value);\n  const bits: bigint = view.getBigUint64(0);\n  view.setBigUint64(0, bits + (value > 0 === upward ? BigInt(1) : -BigInt(1)));\n  return view.getFloat64(0);\n};\n"],"mappings":";;;;;;;;;;;;;;;;;;AAoBA,MAAa,kBACX,QACA,SAAuB,KAAK,WACjB;CACX,MAAM,QAA0B,iBAAiB,MAAM;CACvD,MAAM,QAA0B,iBAAiB,MAAM;CACvD,MAAM,UACJ,OAAO,UAAU,UAAU,OAAO,IAAI,MAAM,QAAQ;CACtD,MAAM,UACJ,OAAO,UAAU,UAAU,OAAO,MAAM,MAAM,QAAQ;CACxD,IAAI,UAAU,SACZ,MAAM,IAAI,MAAM,8CAA8C;CAChE,OAAO,OAAO,eAAe,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