{"record":{"id":"1d17d0d1753948a5","repo":"bchavez/Bogus","slug":"nameof-items-length-and-nameof-weights-lengt","errorCode":null,"errorMessage":"{nameof(items)}.Length and {nameof(weights)}.Length must be the same.","messagePattern":"(.+?)\\.Length and (.+?)\\.Length must be the same\\.","errorType":"exception","errorClass":"ArgumentOutOfRangeException","httpStatus":null,"severity":"error","filePath":"Source/Bogus/Randomizer.cs","lineNumber":847,"sourceCode":"\n   /// <summary>\n   /// Generates a random hexadecimal string.\n   /// </summary>\n   public string Hexadecimal(int length = 1, string prefix = \"0x\")\n   {\n      var sb = new StringBuilder();\n      return Enumerable.Range(1, length).Aggregate(sb, (b, i) => b.Append(ArrayElement(HexChars)), b => $\"{prefix}{b}\");\n   }\n\n   //items are weighted by the decimal probability in their value\n   /// <summary>\n   /// Returns a selection of T[] based on a weighted distribution of probability.\n   /// </summary>\n   /// <param name=\"items\">Items to draw the selection from.</param>\n   /// <param name=\"weights\">Weights in decimal form: IE:[.25, .50, .25] for total of 3 items. Should add up to 1.</param>\n   public T WeightedRandom<T>(T[] items, float[] weights)\n   {\n      if( weights.Length != items.Length ) throw new ArgumentOutOfRangeException($\"{nameof(items)}.Length and {nameof(weights)}.Length must be the same.\");\n\n      var rand = this.Float();\n      float max;\n      float min = 0f;\n\n      var item = default(T);\n\n      for( int i = 0; i < weights.Length; i++ )\n      {\n         max = min + weights[i];\n         item = items[i];\n         if( rand >= min && rand <= max )\n         {\n            break;\n         }\n         min = min + weights[i];\n      }\n","sourceCodeStart":829,"sourceCodeEnd":865,"githubUrl":"https://github.com/bchavez/Bogus/blob/6ece18c5c22648ab4606e45a0aaf2f07fc5259e4/Source/Bogus/Randomizer.cs#L829-L865","documentation":"Thrown by Randomizer.WeightedRandom when weights.Length != items.Length. Weighted sampling pairs each item with a weight by index, so the two arrays must be the same length. Note: the message references both Lengths but throws ArgumentOutOfRangeException.","triggerScenarios":"Calling f.Random.WeightedRandom(items, weights) where the two arrays have different element counts.","commonSituations":"Building items and weights from separate sources and missing a sync; editing one list and forgetting the other.","solutions":["Ensure weights.Length == items.Length before calling.","Generate weights from items with a single pass so they stay aligned.","Add a debug assert: System.Diagnostics.Debug.Assert(weights.Length == items.Length)."],"exampleFix":"// before\nvar pick = f.Random.WeightedRandom(items, weights);\n// after\nif (weights.Length != items.Length) throw new InvalidOperationException(\"length mismatch\");\nvar pick = f.Random.WeightedRandom(items, weights);","handlingStrategy":"validation","validationCode":"static T SafeWeighted<T>(Bogus.Randomizer r, T[] items, float[] weights) {\n    if (weights.Length != items.Length) throw new InvalidOperationException(\"items/weights length mismatch\");\n    return r.WeightedRandom(items, weights);\n}","typeGuard":"bool SameLength<T>(T[] items, float[] weights) => items.Length == weights.Length;","tryCatchPattern":"try { return r.WeightedRandom(items, weights); }\ncatch (ArgumentOutOfRangeException ex) when (ex.Message.Contains(\"Length must be the same\")) {\n    // resize/normalize weights to match items, then retry\n}","preventionTips":["Build items and weights together to keep alignment.","Assert weights.Length == items.Length in debug.","Normalize weights to sum to 1 to avoid sampling bias."],"tags":["bogus","randomizer","weighted","length-mismatch"],"backgroundTag":null,"analyzedSha":"6ece18c5c22648ab4606e45a0aaf2f07fc5259e4","analyzedAt":"2026-08-13T21:08:15.463Z","schemaVersion":2},"datasetVersion":"2026-08-14T00:17:13.853Z"}