{"record":{"id":"833a6efa77b39cc1","repo":"bchavez/Bogus","slug":"nameof-ornull-nameof-nullweight-of-null","errorCode":null,"errorMessage":".{nameof(OrNull)}() {nameof(nullWeight)} of '{nullWeight}' must be between 1.0f and 0.0f.","messagePattern":"\\.(.+?)\\(\\) (.+?) of '(.+?)' must be between 1\\.0f and 0\\.0f\\.","errorType":"exception","errorClass":"ArgumentOutOfRangeException","httpStatus":null,"severity":"error","filePath":"Source/Bogus/Faker[T].Extensions.cs","lineNumber":35,"sourceCode":"   /// <param name=\"max\">Maximum number of T objects to create. Inclusive.</param>\n   public static List<T> GenerateBetween<T>(this Faker<T> faker, int min, int max, string ruleSets = null) where T : class\n   {\n      var internals = (IFakerTInternal)faker;\n      var r = internals.FakerHub.Random;\n      var n = r.Number(min, max);\n      return faker.Generate(n, ruleSets);\n   }\n\n   /// <summary>\n   /// Helpful extension for creating randomly null values for <seealso cref=\"Faker{T}\"/>.RuleFor() rules.\n   /// Example: .RuleFor(x=>x.Prop, f=>f.Random.Word().OrNull(f))\n   /// </summary>\n   /// <typeparam name=\"T\">Any reference type.</typeparam>\n   /// <param name=\"f\">The Faker facade. This is usually the f from RuleFor(.., f => lambda).</param>\n   /// <param name=\"nullWeight\">The probability of null occurring. Range [1.0f - 0.0f] (100% and 0%) respectively. For example, if 15% null is desired pass nullWeight = 0.15f.</param>\n   public static T OrNull<T>(this T value, in Faker f, float nullWeight = 0.5f) where T : class\n   {\n      if (nullWeight is > 1 or < 0) throw new ArgumentOutOfRangeException(nameof(nullWeight), $\".{nameof(OrNull)}() {nameof(nullWeight)} of '{nullWeight}' must be between 1.0f and 0.0f.\");\n      return f.Random.Float() > nullWeight ? value : null;\n   }\n\n   /// <summary>\n   /// Helpful extension for creating randomly null values for <seealso cref=\"Faker{T}\"/>.RuleFor() rules.\n   /// Example: .RuleFor(x=>x.Prop, f=>f.Random.Int().OrNull(f))\n   /// </summary>\n   /// <typeparam name=\"T\">Any nullable type. ie: int?, Guid?, etc.</typeparam>\n   /// <param name=\"f\">The Faker facade. This is usually the f from RuleFor(.., f => lambda).</param>\n   /// <param name=\"nullWeight\">The probability of null occurring. Range [1.0f - 0.0f] (100% and 0%) respectively. For example, if 15% null is desired pass nullWeight = 0.15f.</param>\n   public static T? OrNull<T>(this T value, Faker f, float nullWeight = 0.5f) where T : struct\n   {\n      if (nullWeight is > 1 or < 0) throw new ArgumentOutOfRangeException(nameof(nullWeight), $\".{nameof(OrNull)}() {nameof(nullWeight)} of '{nullWeight}' must be between 1.0f and 0.0f.\");\n      return f.Random.Float() > nullWeight ? new T?(value) : null;\n   }\n\n   /// <summary>\n   /// Helpful extension for creating randomly default(T) values for <seealso cref=\"Faker{T}\"/>.RuleFor() rules.","sourceCodeStart":17,"sourceCodeEnd":53,"githubUrl":"https://github.com/bchavez/Bogus/blob/6ece18c5c22648ab4606e45a0aaf2f07fc5259e4/Source/Bogus/Faker[T].Extensions.cs#L17-L53","documentation":"OrNull<T>(this T value, in Faker f, float nullWeight) for reference types throws ArgumentOutOfRangeException if nullWeight is below 0 or above 1. nullWeight is the probability of returning null, so it must lie in [0.0, 1.0]. Default is 0.5.","triggerScenarios":"Calling f.Random.Word().OrNull(f, 1.5f) or .OrNull(f, -0.2f) on a reference-type value in a RuleFor lambda.","commonSituations":"Passing a percentage (e.g. 15) instead of a fraction (0.15); passing a computed weight that under/overflows the [0,1] band.","solutions":["Pass a fraction in [0.0, 1.0]; for 15% null use 0.15f.","Clamp the weight before calling: Math.Clamp(weight, 0f, 1f).","Omit the argument to use the 0.5 default."],"exampleFix":"// before\n.RuleFor(x => x.Name, f => f.Name.FullName().OrNull(f, 15f))\n\n// after\n.RuleFor(x => x.Name, f => f.Name.FullName().OrNull(f, 0.15f))","handlingStrategy":"validation","validationCode":"float nullWeight = Math.Clamp(rawWeight, 0f, 1f);\nvar v = f.Random.Word().OrNull(f, nullWeight);","typeGuard":"static bool IsValidWeight(float w) => w >= 0f && w <= 1f;","tryCatchPattern":null,"preventionTips":["Pass a fraction (0.15), not a percent (15).","Clamp externally supplied weights to [0,1].","Omit nullWeight to use the 0.5 default."],"tags":["faker","ornull","argument-validation","probability"],"backgroundTag":null,"analyzedSha":"6ece18c5c22648ab4606e45a0aaf2f07fc5259e4","analyzedAt":"2026-08-13T21:08:15.463Z","schemaVersion":2},"datasetVersion":"2026-08-14T00:17:13.853Z"}