{"version":3,"file":"_randomNumber.mjs","names":[],"sources":["../../src/internal/_randomNumber.ts"],"sourcesContent":["import { OpenApi } from \"@typia/interface\";\n\nimport { _randomMultiple } from \"./_randomMultiple\";\n\n/**\n * Generate a random number from a number schema.\n *\n * The stricter of the inclusive and exclusive bounds wins, a missing side\n * defaults to a window of one hundred, 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 selected from the inclusive and exclusive values with the stricter winning, a missing side defaults to a window of one hundred, the source fraction is scaled into the selected interval using weighted finite endpoints when subtraction overflows; an exclusive bound hit exactly uses an interior midpoint or an exact adjacent binary64 value, and a step is handled by the decimal multiple generator.\n * @evidence contracts/common.md#clear-and-simple-design One function with private boundary helpers.\n * @evidence contracts/common.md#prohibited-implementation-shortcuts Errors are explicit for empty and infinite ranges.\n * @evidence contracts/common.md#meaningful-documentation The boundary helper explains the infinite bound rule.\n */\nexport const _randomNumber = (\n  schema: OpenApi.IJsonSchema.INumber,\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(\n        {\n          minimum,\n          maximum,\n          exclusiveMinimum: lower?.exclusive ?? false,\n          exclusiveMaximum: upper?.exclusive ?? false,\n        },\n        source,\n      )\n    : _randomMultiple(\n        {\n          minimum,\n          maximum,\n          multipleOf: schema.multipleOf,\n          exclusiveMinimum: lower?.exclusive ?? false,\n          exclusiveMaximum: upper?.exclusive ?? false,\n          integer: false,\n        },\n        source,\n      );\n};\n\nconst scalar = (\n  props: {\n    minimum: number;\n    maximum: number;\n    exclusiveMinimum: boolean;\n    exclusiveMaximum: boolean;\n  },\n  source: () => number,\n): number => {\n  if (\n    props.minimum === props.maximum &&\n    (props.exclusiveMinimum || props.exclusiveMaximum)\n  )\n    throw new Error(\"Exclusive numeric range is empty.\");\n  const draw: number = source();\n  const width: number = props.maximum - props.minimum;\n  const value: number = Math.max(\n    props.minimum,\n    Math.min(\n      props.maximum,\n      Number.isFinite(width)\n        ? draw * width + props.minimum\n        : (1 - draw) * props.minimum + draw * props.maximum,\n    ),\n  );\n  if (\n    (props.exclusiveMinimum && value === props.minimum) ||\n    (props.exclusiveMaximum && value === props.maximum)\n  ) {\n    const middle: number = props.minimum / 2 + props.maximum / 2;\n    if (middle > props.minimum && middle < props.maximum) return middle;\n    const adjacent: number = nextRepresentable(value, value === props.minimum);\n    if (\n      !Number.isFinite(adjacent) ||\n      adjacent < props.minimum ||\n      adjacent > props.maximum ||\n      (props.exclusiveMinimum && adjacent === props.minimum) ||\n      (props.exclusiveMaximum && adjacent === props.maximum)\n    )\n      throw new Error(\"Exclusive numeric range has no representable value.\");\n    return adjacent;\n  }\n  return value;\n};\n\nconst getLowerBoundary = (\n  schema: OpenApi.IJsonSchema.INumber,\n): IBoundary | null =>\n  selectBoundary(\n    boundary(schema.minimum, false, \"lower\"),\n    boundary(schema.exclusiveMinimum, true, \"lower\"),\n    Math.max,\n  );\n\nconst getUpperBoundary = (\n  schema: OpenApi.IJsonSchema.INumber,\n): IBoundary | null =>\n  selectBoundary(\n    boundary(schema.maximum, false, \"upper\"),\n    boundary(schema.exclusiveMaximum, true, \"upper\"),\n    Math.min,\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,iBACX,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,KAAA,IACzB,OACE;EACE;EACA;EACA,kBAAkB,OAAO,aAAa;EACtC,kBAAkB,OAAO,aAAa;CACxC,GACA,MACF,IACA,gBACE;EACE;EACA;EACA,YAAY,OAAO;EACnB,kBAAkB,OAAO,aAAa;EACtC,kBAAkB,OAAO,aAAa;EACtC,SAAS;CACX,GACA,MACF;AACN;AAEA,MAAM,UAC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