{"record":{"id":"810b0678b84683fd","repo":"stanfordnlp/CoreNLP","slug":"can-t-sample-from-nan","errorCode":null,"errorMessage":"Can't sample from NaN","messagePattern":"Can't sample from NaN","errorType":"exception","errorClass":"RuntimeException","httpStatus":null,"severity":"error","filePath":"src/edu/stanford/nlp/math/ArrayMath.java","lineNumber":1464,"sourceCode":"  public static int sampleFromDistribution(double[] d) {\n    return sampleFromDistribution(d, rand);\n  }\n\n  /**\n   * Samples from the distribution over values 0 through d.length given by d.\n   * Assumes that the distribution sums to 1.0.\n   *\n   * @param d the distribution to sample from\n   * @return a value from 0 to d.length\n   */\n  public static int sampleFromDistribution(double[] d, Random random) {\n    // sample from the uniform [0,1]\n    double r = random.nextDouble();\n    // now compare its value to cumulative values to find what interval it falls in\n    double total = 0;\n    for (int i = 0; i < d.length - 1; i++) {\n      if (Double.isNaN(d[i])) {\n        throw new RuntimeException(\"Can't sample from NaN\");\n      }\n      total += d[i];\n      if (r < total) {\n        return i;\n      }\n    }\n    return d.length - 1; // in case the \"double-math\" didn't total to exactly 1.0\n  }\n\n  /**\n   * Samples from the distribution over values 0 through d.length given by d.\n   * Assumes that the distribution sums to 1.0.\n   *\n   * @param d the distribution to sample from\n   * @return a value from 0 to d.length\n   */\n  public static int sampleFromDistribution(float[] d, Random random) {\n    // sample from the uniform [0,1]","sourceCodeStart":1446,"sourceCodeEnd":1482,"githubUrl":"https://github.com/stanfordnlp/CoreNLP/blob/1b7edd19c4d0d7b1f13a2591425b9b60a0b1af7a/src/edu/stanford/nlp/math/ArrayMath.java#L1446-L1482","documentation":"ArrayMath.sampleFromDistribution(double[], Random) draws an index according to the distribution d. If any entry (except the last, treated as remainder) is NaN, cumulative comparison becomes meaningless, so a RuntimeException(\"Can't sample from NaN\") is thrown while walking the cumulative sums.","triggerScenarios":"Calling ArrayMath.sampleFromDistribution(double[] d, Random r) where any d[i] for i < d.length - 1 is NaN — typically from an unnormalized or degenerate probability computation upstream.","commonSituations":"Sampling from a softmax/language-model output distribution that was computed from Inf/NaN logits, or from probabilities that were divided by a zero total mass.","solutions":["Validate the distribution before sampling: reject or repair arrays containing NaN (e.g. rebuild from a uniform distribution as fallback).","Check the upstream probability computation (softmax inputs, normalization constant) for overflow or zero-sum issues.","Replace NaN entries with 0.0 or renormalize with ArrayMath.normalize(d) prior to sampling when NaNs indicate dead outcomes.","Catch RuntimeException and fall back to a uniform or argmax choice if occasional degenerate distributions are tolerable."],"exampleFix":"// before\nint i = ArrayMath.sampleFromDistribution(probs, rand);\n// after\nboolean clean = true;\nfor (double p : probs) { if (Double.isNaN(p)) { clean = false; break; } }\nint i = clean ? ArrayMath.sampleFromDistribution(probs, rand) : rand.nextInt(probs.length);","handlingStrategy":"validation","validationCode":"boolean clean = true;\nfor (int i = 0; i < d.length - 1; i++) {\n  if (Double.isNaN(d[i])) { clean = false; break; }\n}\nif (!clean) { /* rebuild distribution or use uniform fallback */ }","typeGuard":null,"tryCatchPattern":"try {\n  idx = ArrayMath.sampleFromDistribution(d, rand);\n} catch (RuntimeException e) {\n  idx = rand.nextInt(d.length); // uniform fallback\n}","preventionTips":["Validate distributions for NaN before sampling","Stabilize softmax computations (subtract max) to avoid NaN","Renormalize with ArrayMath.normalize before sampling","Define a fallback policy (uniform/argmax) for degenerate distributions"],"tags":["math","nan","sampling","probability-distribution"],"backgroundTag":"invalid-argument-value","analyzedSha":"1b7edd19c4d0d7b1f13a2591425b9b60a0b1af7a","analyzedAt":"2026-09-10T02:24:07.274Z","contentChangedAt":"2026-09-10T02:24:07.274Z","schemaVersion":2},"datasetVersion":"2026-09-17T15:17:12.973Z"}