| | | 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 | | namespace GDX.Mathematics.Random |
| | | 6 | | { |
| | | 7 | | public class RandomWrapper : IRandomProvider |
| | | 8 | | { |
| | | 9 | | readonly System.Random m_Random; |
| | | 10 | | |
| | 1 | 11 | | public RandomWrapper() |
| | 1 | 12 | | { |
| | 1 | 13 | | m_Random = new System.Random(); |
| | 1 | 14 | | } |
| | | 15 | | |
| | 14 | 16 | | public RandomWrapper(int seed) |
| | 14 | 17 | | { |
| | 14 | 18 | | m_Random = new System.Random(seed); |
| | 14 | 19 | | } |
| | | 20 | | |
| | | 21 | | /// <inheritdoc /> |
| | | 22 | | public bool NextBoolean(float chance = 0.5f) |
| | 2 | 23 | | { |
| | 2 | 24 | | return m_Random.NextDouble() <= chance; |
| | 2 | 25 | | } |
| | | 26 | | |
| | | 27 | | /// <inheritdoc /> |
| | | 28 | | public void NextBytes(byte[] buffer) |
| | 1 | 29 | | { |
| | 1 | 30 | | m_Random.NextBytes(buffer); |
| | 1 | 31 | | } |
| | | 32 | | |
| | | 33 | | /// <inheritdoc /> |
| | | 34 | | public double NextDouble(double minValue = 0, double maxValue = 1) |
| | 1 | 35 | | { |
| | 1 | 36 | | return Range.GetDouble(Sample(), minValue, maxValue); |
| | 1 | 37 | | } |
| | | 38 | | |
| | | 39 | | /// <inheritdoc /> |
| | | 40 | | public int NextInteger(int minValue = 0, int maxValue = int.MaxValue) |
| | 25 | 41 | | { |
| | 25 | 42 | | return Range.GetInteger(Sample(), minValue, maxValue); |
| | 25 | 43 | | } |
| | | 44 | | |
| | | 45 | | /// <inheritdoc /> |
| | | 46 | | public int NextIntegerExclusive(int minValue = 0, int maxValue = int.MaxValue) |
| | 1 | 47 | | { |
| | 1 | 48 | | return Range.GetInteger(Sample(), minValue + 1, maxValue - 1); |
| | 1 | 49 | | } |
| | | 50 | | |
| | | 51 | | /// <inheritdoc /> |
| | | 52 | | public float NextSingle(float minValue = 0, float maxValue = 1) |
| | 1 | 53 | | { |
| | 1 | 54 | | return Range.GetSingle(Sample(), minValue, maxValue); |
| | 1 | 55 | | } |
| | | 56 | | |
| | | 57 | | /// <inheritdoc /> |
| | | 58 | | public uint NextUnsignedInteger(uint minValue = uint.MinValue, uint maxValue = uint.MaxValue) |
| | 1 | 59 | | { |
| | 1 | 60 | | return Range.GetUnsignedInteger(Sample(), minValue, maxValue); |
| | 1 | 61 | | } |
| | | 62 | | |
| | | 63 | | /// <inheritdoc /> |
| | | 64 | | public uint NextUnsignedIntegerExclusive(uint minValue = uint.MinValue, uint maxValue = uint.MaxValue) |
| | 1 | 65 | | { |
| | 1 | 66 | | return Range.GetUnsignedInteger(Sample(), minValue + 1, maxValue - 1); |
| | 1 | 67 | | } |
| | | 68 | | |
| | | 69 | | /// <inheritdoc /> |
| | | 70 | | public double Sample() |
| | 30 | 71 | | { |
| | 30 | 72 | | return m_Random.NextDouble(); |
| | 30 | 73 | | } |
| | | 74 | | } |
| | | 75 | | } |