bchavez/Bogus · error · ArgumentOutOfRangeException
.{nameof(OrDefault)}() {nameof(defaultWeight)} of '{defaultW
Error message
.{nameof(OrDefault)}() {nameof(defaultWeight)} of '{defaultWeight}' must be between 1.0f and 0.0f. What it means
OrDefault<T>(this T value, Faker f, float defaultWeight, T defaultValue) throws ArgumentOutOfRangeException if defaultWeight is below 0 or above 1. defaultWeight is the probability of returning defaultValue instead of the generated value; valid range is [0.0, 1.0], default 0.5.
Source
Thrown at Source/Bogus/Faker[T].Extensions.cs:61
/// <typeparam name="T">Any nullable type. ie: int?, Guid?, etc.</typeparam>
/// <param name="f">The Faker facade. This is usually the f from RuleFor(.., f => lambda).</param>
/// <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>
public static T? OrNull<T>(this T value, Faker f, float nullWeight = 0.5f) where T : struct
{
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.");
return f.Random.Float() > nullWeight ? new T?(value) : null;
}
/// <summary>
/// Helpful extension for creating randomly default(T) values for <seealso cref="Faker{T}"/>.RuleFor() rules.
/// Example: .RuleFor(x=>x.Prop, f=>f.Random.Word().OrDefault(f))
/// </summary>
/// <param name="f">The Faker facade. This is usually the f from f => lambda.</param>
/// <param name="defaultWeight">The probability of the default value occurring. Range [1.0f - 0.0f] (100% and 0%) respectively. For example, if 15% default(T) is desired pass defaultWeight = 0.15f.</param>
/// <param name="defaultValue">The default value to return.</param>
public static T OrDefault<T>(this T value, Faker f, float defaultWeight = 0.5f, T defaultValue = default)
{
if (defaultWeight is > 1 or < 0) throw new ArgumentOutOfRangeException(nameof(defaultWeight), $".{nameof(OrDefault)}() {nameof(defaultWeight)} of '{defaultWeight}' must be between 1.0f and 0.0f. ");
return f.Random.Float() > defaultWeight ? value : defaultValue;
}
}View on GitHub (pinned to 6ece18c5c2)
Solutions
- Pass a fraction in [0.0, 1.0]; for 30% default use 0.3f.
- Clamp with Math.Clamp(weight, 0f, 1f) before calling.
- Omit the argument to use the 0.5 default.
Example fix
// before .RuleFor(x => x.Code, f => f.Random.AlphaNumeric(5).OrDefault(f, 30f)) // after .RuleFor(x => x.Code, f => f.Random.AlphaNumeric(5).OrDefault(f, 0.3f))
Defensive patterns
Strategy: validation
Validate before calling
float defaultWeight = Math.Clamp(rawWeight, 0f, 1f); var v = f.Random.AlphaNumeric(5).OrDefault(f, defaultWeight);
Type guard
static bool IsValidWeight(float w) => w >= 0f && w <= 1f;
Prevention
- Pass a fraction (0.3), not a percent (30).
- Clamp externally supplied weights to [0,1].
- Omit defaultWeight to use the 0.5 default.
When it happens
Trigger: Calling f.Random.Word().OrDefault(f, 1.2f) or .OrDefault(f, -0.5f) in a RuleFor lambda.
Common situations: Passing a percentage (e.g. 30) instead of a fraction (0.3), or an unclamped weight from configuration.
Related errors
- .{nameof(OrNull)}() {nameof(nullWeight)} of '{nullWeight}' m
- The specified property '{property.Name}' does not have a set
- {nameof(amountToPick)} needs to be a positive integer.
- {nameof(amountToPick)} is greater than the number of items.
- The locale '{locale}' does not exist. To see all available l
AI-assisted analysis of bchavez/Bogus@6ece18c5c2 (2026-08-13).
Data as JSON: /api/errors/25be86203917f629.
Report an issue: GitHub.