Class RandomExtensions
Random class extension methods.
Namespace: Neon.Common
Assembly: Neon.Common.dll
Syntax
public static class RandomExtensions
Methods
NextIndex(Random, int)
Returns a random index into a sequence whose length is specified.
Declaration
public static int NextIndex(this Random random, int length)
Parameters
| Type | Name | Description |
|---|---|---|
| Random | random | The Random instance. |
| int | length | The sequence length. |
Returns
| Type | Description |
|---|---|
| int | The random index. |
Exceptions
| Type | Condition |
|---|---|
| IndexOutOfRangeException | Thrown if length is <= 0. |
RandomTimespan(Random, TimeSpan)
Returns a random TimeSpan between zero and a specified maximum.
Declaration
public static TimeSpan RandomTimespan(this Random random, TimeSpan maxInterval)
Parameters
| Type | Name | Description |
|---|---|---|
| Random | random | The Random instance. |
| TimeSpan | maxInterval | The maximum interval. |
Returns
| Type | Description |
|---|---|
| TimeSpan | The random timespan. |
Remarks
This method is useful for situations where its desirable to have some variation in a delay before performing an activity like retrying an operation or performing a background task.
RandomTimespan(Random, TimeSpan, double)
Returns a TimeSpan between the specified base interval plus a random period of the specified fraction of the value.
Declaration
public static TimeSpan RandomTimespan(this Random random, TimeSpan baseInterval, double fraction)
Parameters
| Type | Name | Description |
|---|---|---|
| Random | random | The Random instance. |
| TimeSpan | baseInterval | The base interval. |
| double | fraction | The fractional multiplier for the random component. |
Returns
| Type | Description |
|---|---|
| TimeSpan | The random timespan. |
Remarks
The value returned is at least as large as baseInterval with an
added random fractional interval if fraction is positive or the value
returned may be less than baseInterval for a negative fraction.
This is computed via:
baseInterval + RandTimespan(TimeSpan.FromSeconds(baseInterval.TotalSeconds * fraction));
This method is useful for situations where its desirable to have some variation in a delay before performing an activity like retrying an operation or performing a background task.