| | | 1 | | // ----------------------------------------------------------------------- |
| | | 2 | | // <copyright file="RandomGeneratorExtensions.cs" company="Stéphane ANDRE"> |
| | | 3 | | // Copyright (c) Stéphane ANDRE. All rights reserved. |
| | | 4 | | // </copyright> |
| | | 5 | | // ----------------------------------------------------------------------- |
| | | 6 | | |
| | | 7 | | using System; |
| | | 8 | | using System.Collections.Generic; |
| | | 9 | | using MyNet.Primitives; |
| | | 10 | | using MyNet.Primitives.Intervals; |
| | | 11 | | |
| | | 12 | | #pragma warning disable IDE0130 // Namespace does not match folder structure |
| | | 13 | | namespace MyNet.Generator; |
| | | 14 | | #pragma warning restore IDE0130 // Namespace does not match folder structure |
| | | 15 | | |
| | | 16 | | public static class RandomGeneratorExtensions |
| | | 17 | | { |
| | | 18 | | extension(IRandomGenerator random) |
| | | 19 | | { |
| | | 20 | | /// <summary> |
| | | 21 | | /// Generates a random subset of the provided list with a specified count. The method first checks if the input |
| | | 22 | | /// </summary> |
| | | 23 | | /// <param name="list">The list from which to generate a random subset.</param> |
| | | 24 | | /// <param name="count">The number of items to include in the subset.</param> |
| | | 25 | | /// <typeparam name="T">The type of items in the list.</typeparam> |
| | | 26 | | /// <returns>A random subset of the list with the specified count.</returns> |
| | | 27 | | public IReadOnlyList<T> SafeSubset<T>(IReadOnlyCollection<T> list, int count) |
| | | 28 | | { |
| | 3 | 29 | | ArgumentNullException.ThrowIfNull(list); |
| | | 30 | | |
| | 3 | 31 | | var safeCount = count.SafeClamp(0, list.Count); |
| | | 32 | | |
| | 3 | 33 | | return random.Subset(list, safeCount); |
| | | 34 | | } |
| | | 35 | | |
| | | 36 | | /// <summary> |
| | | 37 | | /// Attempts to retrieve a random item from the provided list. If the list is empty, it returns the default valu |
| | | 38 | | /// </summary> |
| | | 39 | | /// <param name="list">The list from which to retrieve a random item.</param> |
| | | 40 | | /// <typeparam name="T">The type of items in the list.</typeparam> |
| | | 41 | | /// <returns>A random item from the list, or the default value if the list is empty.</returns> |
| | | 42 | | /// <exception cref="ArgumentNullException">Thrown if the list is null.</exception> |
| | | 43 | | public T? TryItem<T>(IReadOnlyCollection<T> list) |
| | | 44 | | { |
| | 6 | 45 | | ArgumentNullException.ThrowIfNull(list); |
| | | 46 | | |
| | 6 | 47 | | return list.Count == 0 ? default : random.Item(list); |
| | | 48 | | } |
| | | 49 | | |
| | | 50 | | /// <summary> |
| | | 51 | | /// Generates a random DateTime value that falls within the specified interval. The method extracts the start an |
| | | 52 | | /// </summary> |
| | | 53 | | /// <param name="interval">The interval specifying the start and end DateTime values.</param> |
| | | 54 | | /// <returns>A random DateTime value within the specified interval.</returns> |
| | 60 | 55 | | public DateTime Date(IInterval<DateTime> interval) => random.SafeDate(interval.Start?.Value, interval.End?.Value |
| | | 56 | | |
| | | 57 | | /// <summary> |
| | | 58 | | /// Generates a random DateTime value that falls within the specified minimum and maximum range. If the minimum |
| | | 59 | | /// </summary> |
| | | 60 | | /// <param name="min">The minimum DateTime value.</param> |
| | | 61 | | /// <param name="max">The maximum DateTime value.</param> |
| | | 62 | | /// <returns>A random DateTime value within the specified range.</returns> |
| | 0 | 63 | | public DateTime Date(DateTime? min = null, DateTime? max = null) => random.SafeDate(min, max); |
| | | 64 | | |
| | | 65 | | /// <summary> |
| | | 66 | | /// Generates a random DateTime value that falls within the specified minimum and maximum range, ensuring that t |
| | | 67 | | /// </summary> |
| | | 68 | | /// <param name="min">The minimum DateTime value.</param> |
| | | 69 | | /// <param name="max">The maximum DateTime value.</param> |
| | | 70 | | /// <returns>A random DateTime value within the specified range.</returns> |
| | | 71 | | public DateTime SafeDate(DateTime? min = null, DateTime? max = null) |
| | | 72 | | { |
| | 63 | 73 | | var minMax = min.MinMax(max); |
| | 63 | 74 | | return GenerateDate( |
| | 63 | 75 | | random, |
| | 63 | 76 | | minMax.Min ?? DateTime.MinValue, |
| | 63 | 77 | | minMax.Max ?? DateTime.MaxValue); |
| | | 78 | | } |
| | | 79 | | } |
| | | 80 | | |
| | | 81 | | private static DateTime GenerateDate(IRandomGenerator generator, DateTime minimum, DateTime maximum) |
| | 63 | 82 | | => generator.Date(minimum, maximum); |
| | | 83 | | } |
| | | 84 | | |