| | 1 | | // Copyright (c) 2020-2024 dotBunny Inc. |
| | 2 | | // dotBunny licenses this file to you under the BSL-1.0 license. |
| | 3 | | // See the LICENSE file in the project root for more information. |
| | 4 | |
|
| | 5 | | using System.Diagnostics.CodeAnalysis; |
| | 6 | |
|
| | 7 | | namespace GDX.Mathematics.Random |
| | 8 | | { |
| | 9 | | // ReSharper disable CommentTypo |
| | 10 | | /// <summary> |
| | 11 | | /// Adapter to utilize a <see cref="IRandomProvider" /> with <see cref="System.Random" /> based systems, wrappin |
| | 12 | | /// provider in a class object with expected overrides. |
| | 13 | | /// </summary> |
| | 14 | | /// <remarks>This will create IL <c>callvert</c> operation codes! Try not to use this.</remarks> |
| | 15 | | // ReSharper restore CommentTypo |
| | 16 | | [VisualScriptingCompatible(4)] |
| | 17 | | public class RandomAdaptor : System.Random |
| | 18 | | { |
| | 19 | | readonly IRandomProvider m_Provider; |
| | 20 | |
|
| | 21 | | /// <summary> |
| | 22 | | /// Create an adaptor object which behaves like <see cref="System.Random" />. |
| | 23 | | /// </summary> |
| | 24 | | /// <remarks> |
| | 25 | | /// Will cause boxing of <see langword="struct" /> based types like <see cref="WELL1024a" />. |
| | 26 | | /// This adaptor really should only be used where a method is expecting a <see cref="System.Random" />. |
| | 27 | | /// </remarks> |
| | 28 | | /// <param name="provider">A qualified <see cref="IRandomProvider" />.</param> |
| 7 | 29 | | public RandomAdaptor(IRandomProvider provider) |
| 7 | 30 | | { |
| 7 | 31 | | m_Provider = provider; |
| 7 | 32 | | } |
| | 33 | |
|
| | 34 | | /// <summary> |
| | 35 | | /// Is the provider present, and not null? |
| | 36 | | /// </summary> |
| | 37 | | /// <returns>true/false a provider is not null.</returns> |
| | 38 | | public bool HasProvider() |
| 1 | 39 | | { |
| 1 | 40 | | return m_Provider != null; |
| 1 | 41 | | } |
| | 42 | |
|
| | 43 | | [ExcludeFromCodeCoverage] |
| | 44 | | protected override double Sample() |
| | 45 | | { |
| | 46 | | return m_Provider.Sample(); |
| | 47 | | } |
| | 48 | |
|
| | 49 | | public override int Next() |
| 6 | 50 | | { |
| 6 | 51 | | return m_Provider.NextInteger(); |
| 6 | 52 | | } |
| | 53 | |
|
| | 54 | | public override int Next(int minValue, int maxValue) |
| 3 | 55 | | { |
| 3 | 56 | | if (maxValue != int.MaxValue) |
| 3 | 57 | | { |
| 3 | 58 | | maxValue++; |
| 3 | 59 | | } |
| | 60 | |
|
| 3 | 61 | | return m_Provider.NextInteger(minValue, maxValue); |
| 3 | 62 | | } |
| | 63 | |
|
| | 64 | | public override int Next(int maxValue) |
| 3 | 65 | | { |
| 3 | 66 | | if (maxValue != int.MaxValue) |
| 3 | 67 | | { |
| 3 | 68 | | maxValue++; |
| 3 | 69 | | } |
| | 70 | |
|
| 3 | 71 | | return m_Provider.NextInteger(0, maxValue); |
| 3 | 72 | | } |
| | 73 | |
|
| | 74 | | public override double NextDouble() |
| 2 | 75 | | { |
| 2 | 76 | | return m_Provider.NextDouble(); |
| 2 | 77 | | } |
| | 78 | |
|
| | 79 | | public override void NextBytes(byte[] buffer) |
| 1 | 80 | | { |
| 1 | 81 | | m_Provider.NextBytes(buffer); |
| 1 | 82 | | } |
| | 83 | | } |
| | 84 | | } |