|
1 | 1 | # Random-Generators |
2 | | -A collection of random generators and distributions. WIP. |
| 2 | +Random generators for Unity is a collection of random generators, |
| 3 | +different distributions, modificators and filters. |
| 4 | + |
| 5 | +The library has a useful infrastructure that makes it easy to expand it and add new |
| 6 | +distributions, modificators, filters and random generators. |
| 7 | + |
| 8 | +The library is very fast and heap allocation free. |
| 9 | + |
| 10 | +## Installation |
| 11 | + |
| 12 | +This repo is a regular Unity package. You can install it as your project dependency. |
| 13 | +More here: https://docs.unity3d.com/Manual/upm-dependencies.html. |
| 14 | + |
| 15 | +## Usage |
| 16 | + |
| 17 | +1. Create a provider from **Assets/Create/Random Generator Providers/** or choose one of |
| 18 | +pre-made providers among |
| 19 | +**DefaultBoolUniformGeneratorProvider**, **DefaultIntUniformGeneratorProvider**, |
| 20 | +**DefaultFloatUniformGeneratorProvider**, **DefaultMarsagliaGeneratorProvider** and |
| 21 | +**DefaultBatesGeneratorProvider**. |
| 22 | +2. Add **ContinuousGeneratorProviderReference** for continuous distributions or |
| 23 | +**DiscreteGeneratorProviderReference** for discrete distributions as a serialize field |
| 24 | +into your component. |
| 25 | +3. Link a selected provider into a right provider reference. Toggle on/off Shared. |
| 26 | +If Shared is on, a generator is created once and reused by all requesters. |
| 27 | +If Shared is off, a new generator is created for every requester. |
| 28 | +4. In your script, call **ContinuousGeneratorProviderReference.generator** or |
| 29 | +**DiscreteGeneratorProviderReference.GetGenerator<T>()** and cache the result. |
| 30 | +They return a **IContinuousGenerator** or **IDiscreteGenerator<T>** that generate |
| 31 | +a random value. |
| 32 | +5. Call **Generate()** in a gotten generator to get a random value that corresponds to |
| 33 | +a selected distribution. |
| 34 | + |
| 35 | +Also, you can create your own infrastructure. Every part of this library is public and |
| 36 | +available separately from other parts. |
| 37 | + |
| 38 | +## Parts |
| 39 | + |
| 40 | +### Distributions |
| 41 | + |
| 42 | +Distributions are just algorithms in static classes that return a random value(s). |
| 43 | +They usually require an independent and identically distributed random generator. |
| 44 | +By default, Unity generator is used as such a generator. |
| 45 | +Also, the distributions support `Func<float>` and `IContinuousGenerator` as an iid random generator. |
| 46 | + |
| 47 | +### Generators |
| 48 | + |
| 49 | +Generators are standard c# classes that implement [IContinuousGenerator](https://github.com/ZorPastaman/Random-Generators/blob/master/Runtime/ContinuousDistributions/IContinuousGenerator.cs) |
| 50 | +or [IDiscreteGenerator<T>](https://github.com/ZorPastaman/Random-Generators/blob/master/Runtime/DiscreteDistributions/IDiscreteGenerator.cs) |
| 51 | +and wrap one of the methods of the distributions. |
| 52 | + |
| 53 | +### Generator Providers |
| 54 | + |
| 55 | +Generator providers are scriptable objects and can be linked to a serialize field in Unity components. |
| 56 | +They wrap generators and provide unique and shared instances of them. |
| 57 | + |
| 58 | +#### List of continuous generator algorithms |
| 59 | + |
| 60 | +- [Acceptance Rejection](https://github.com/ZorPastaman/Random-Generators/tree/master/Runtime/ContinuousDistributions/IndependentDistributions/AcceptanceRejectionDistribution) - |
| 61 | +[Wikipedia](https://en.wikipedia.org/wiki/Rejection_sampling); |
| 62 | +- [Bates](https://github.com/ZorPastaman/Random-Generators/tree/master/Runtime/ContinuousDistributions/NormalDistributions/BatesDistribution) - |
| 63 | +[Wikipedia](https://en.wikipedia.org/wiki/Bates_distribution); |
| 64 | +- [Box-Muller](https://github.com/ZorPastaman/Random-Generators/tree/master/Runtime/ContinuousDistributions/NormalDistributions/BoxMullerDistribution) - |
| 65 | +[Wikipedia](https://en.wikipedia.org/wiki/Box%E2%80%93Muller_transform); |
| 66 | +- [Irwin-Hall](https://github.com/ZorPastaman/Random-Generators/tree/master/Runtime/ContinuousDistributions/NormalDistributions/IrwinHallDistribution) - |
| 67 | +[Wikipedia](https://en.wikipedia.org/wiki/Irwin%E2%80%93Hall_distribution); |
| 68 | +- [Marsaglia](https://github.com/ZorPastaman/Random-Generators/tree/master/Runtime/ContinuousDistributions/NormalDistributions/MarsagliaDistribution) - |
| 69 | +[Wikipedia](https://en.wikipedia.org/wiki/Marsaglia_polar_method); |
| 70 | +- [C# Random Generator Wrapper](https://github.com/ZorPastaman/Random-Generators/tree/master/Runtime/ContinuousDistributions/UniformDistributions/SharpDistribution) - |
| 71 | +[Microsoft Docs](https://docs.microsoft.com/en-us/dotnet/api/system.random?view=netframework-4.8); |
| 72 | +- [Unity Random Generator Wrapper](https://github.com/ZorPastaman/Random-Generators/tree/master/Runtime/ContinuousDistributions/UniformDistributions/UnityDistribution) - |
| 73 | +[Unity Docs](https://docs.unity3d.com/ScriptReference/Random.html). |
| 74 | + |
| 75 | +#### List of discrete generator algorithms |
| 76 | + |
| 77 | +- [Bernoulli](https://github.com/ZorPastaman/Random-Generators/tree/master/Runtime/DiscreteDistributions/BernoulliDistribution) - |
| 78 | +[Wikipedia](https://en.wikipedia.org/wiki/Bernoulli_distribution); |
| 79 | +- [Binomial](https://github.com/ZorPastaman/Random-Generators/tree/master/Runtime/DiscreteDistributions/BinomialDistribution) - |
| 80 | +[Wikipedia](https://en.wikipedia.org/wiki/Binomial_distribution); |
| 81 | +- [Poisson](https://github.com/ZorPastaman/Random-Generators/tree/master/Runtime/DiscreteDistributions/PoissonDistribution) - |
| 82 | +[Wikipedia](https://en.wikipedia.org/wiki/Poisson_distribution); |
| 83 | +- [C# Random Generator Wrapper](https://github.com/ZorPastaman/Random-Generators/tree/master/Runtime/DiscreteDistributions/SharpDistribution) - |
| 84 | +[Microsoft Docs](https://docs.microsoft.com/en-us/dotnet/api/system.random?view=netframework-4.8); |
| 85 | +- [Unity Random Generator Wrapper](https://github.com/ZorPastaman/Random-Generators/tree/master/Runtime/DiscreteDistributions/UnityDistribution) - |
| 86 | +[Unity Docs](https://docs.unity3d.com/ScriptReference/Random.html); |
| 87 | +- [Weighted](https://github.com/ZorPastaman/Random-Generators/tree/master/Runtime/DiscreteDistributions/WeightedDistribution) - |
| 88 | +[Wikipedia](https://en.wikipedia.org/wiki/Weight_function). |
| 89 | + |
| 90 | +### Modificators |
| 91 | + |
| 92 | +Modificators are standard c# classes that implement [IContinuousGenerator](https://github.com/ZorPastaman/Random-Generators/blob/master/Runtime/ContinuousDistributions/IContinuousGenerator.cs) |
| 93 | +or [IDiscreteGenerator<T>](https://github.com/ZorPastaman/Random-Generators/blob/master/Runtime/DiscreteDistributions/IDiscreteGenerator.cs) |
| 94 | +but they are not actually generators, they take a generated value from a depended generator, modify it somehow and return a result. |
| 95 | + |
| 96 | +### Modificator Providers |
| 97 | + |
| 98 | +Modificator providers are scriptable objects and can be linked to a serialize field in Unity components. |
| 99 | +They wrap modificators and provide unique and shared instances of them. |
| 100 | + |
| 101 | +#### List of continuous modificators |
| 102 | + |
| 103 | +- [Clamp](https://github.com/ZorPastaman/Random-Generators/tree/master/Runtime/ContinuousDistributions/DistributionModificators/Clamp) - |
| 104 | +a continuous value is clamped between specified minimum and maximum values; |
| 105 | +- [Round](https://github.com/ZorPastaman/Random-Generators/tree/master/Runtime/ContinuousDistributions/DistributionModificators/Round) - |
| 106 | +a continuous value is rounded to a nearest integer. |
| 107 | + |
| 108 | +#### List of discrete modificators |
| 109 | + |
| 110 | +- [Clamp](https://github.com/ZorPastaman/Random-Generators/tree/master/Runtime/DiscreteDistributions/DistributionModificators/Clamp) - |
| 111 | +a discrete value is clamped between specified minimum and maximum values; |
| 112 | +- [Round to Int](https://github.com/ZorPastaman/Random-Generators/tree/master/Runtime/DiscreteDistributions/DistributionModificators/Round) - |
| 113 | +a continuous value is rounded to a nearest integer and returned as a discrete value. |
| 114 | + |
| 115 | +### Filters |
| 116 | + |
| 117 | +Filters are algorithms in static classes that check if a new generated value corresponds to their rules. |
| 118 | +They usually forbid certain sequences of random generated values. |
| 119 | + |
| 120 | +### Filter Wrappers |
| 121 | + |
| 122 | +Filter wrappers are standard c# classes that implement [IContinuousFilter](https://github.com/ZorPastaman/Random-Generators/blob/master/Runtime/ContinuousDistributions/DistributionFilters/IContinuousFilter.cs) |
| 123 | +or [IDiscreteFilter](https://github.com/ZorPastaman/Random-Generators/blob/master/Runtime/DiscreteDistributions/DistributionFilters/IDiscreteFilter.cs) |
| 124 | +and wrap one of the methods of filters. |
| 125 | + |
| 126 | +### Filter Providers |
| 127 | + |
| 128 | +Filter providers are scriptable objects and can be linked to a serialize field in Unity components. |
| 129 | +They wrap filter wrappers and provide unique and shared instances of them. |
| 130 | + |
| 131 | +### Filtered Generators |
| 132 | + |
| 133 | +Filtered generators are standard c# classes that implement [IContinuousFilter](https://github.com/ZorPastaman/Random-Generators/blob/master/Runtime/ContinuousDistributions/DistributionFilters/IContinuousFilter.cs) |
| 134 | +or [IDiscreteFilter](https://github.com/ZorPastaman/Random-Generators/blob/master/Runtime/DiscreteDistributions/DistributionFilters/IDiscreteFilter.cs). |
| 135 | +They take a generated value from a depended generator and check that value with filters. |
| 136 | +If at least one filter doesn't approve a new value, it's regenerated and checked again. |
| 137 | + |
| 138 | +### Filtered Generator Providers |
| 139 | + |
| 140 | +Filtered generator providers are scriptable objects and can be linked to a serialize field in Unity components. |
| 141 | +They wrap filtered generators and provide unique and shared instances of them. |
| 142 | + |
| 143 | +#### List of continuous filters |
| 144 | + |
| 145 | +- [Ascendant Sequence](https://github.com/ZorPastaman/Random-Generators/tree/master/Runtime/ContinuousDistributions/DistributionFilters/AscendantSequenceFilters) - |
| 146 | +checks if a value continues an ascendant sequence and it needs to be regenerated; |
| 147 | +- [Close](https://github.com/ZorPastaman/Random-Generators/tree/master/Runtime/ContinuousDistributions/DistributionFilters/CloseFilters) - |
| 148 | +checks if a value continues a sequence where every value is close enough to a reference value and needs to be regenerated; |
| 149 | +- [Descendant Sequence](https://github.com/ZorPastaman/Random-Generators/tree/master/Runtime/ContinuousDistributions/DistributionFilters/DescendantSequenceFilters) - |
| 150 | +checks if a value continues a descendant sequence and it needs to be regenerated; |
| 151 | +- [Greater](https://github.com/ZorPastaman/Random-Generators/tree/master/Runtime/ContinuousDistributions/DistributionFilters/GreaterFilters) - |
| 152 | +checks if a value continues a sequence where every value is greater than a reference value and needs to be regenerated; |
| 153 | +- [In Range](https://github.com/ZorPastaman/Random-Generators/tree/master/Runtime/ContinuousDistributions/DistributionFilters/InRangeFilters) - |
| 154 | +checks if a value continues a sequence where every value is in range between the minimum and maximum and needs to be regenerated; |
| 155 | +- [Less](https://github.com/ZorPastaman/Random-Generators/tree/master/Runtime/ContinuousDistributions/DistributionFilters/LessFilters) - |
| 156 | +checks if a value continues a sequence where every value is less than a reference value and needs to be regenerated; |
| 157 | +- [Little Difference](https://github.com/ZorPastaman/Random-Generators/tree/master/Runtime/ContinuousDistributions/DistributionFilters/LittleDifferenceFilters) - |
| 158 | +checks if a value continues a sequence where consecutive elements differ by less than a required difference and needs to be regenerated; |
| 159 | +- [Not In Range](https://github.com/ZorPastaman/Random-Generators/tree/master/Runtime/ContinuousDistributions/DistributionFilters/NotInRangeFilters) - |
| 160 | +checks if a value continues a sequence where every value is in range between the minimum and maximum and needs to be regenerated. |
| 161 | + |
| 162 | +#### List of discrete filters |
| 163 | + |
| 164 | +- [Ascendant Sequence](https://github.com/ZorPastaman/Random-Generators/tree/master/Runtime/DiscreteDistributions/DistributionFilters/AscendantSequenceFilters) - |
| 165 | +checks if a value continues an ascendant sequence and it needs to be regenerated; |
| 166 | +- [Close](https://github.com/ZorPastaman/Random-Generators/tree/master/Runtime/DiscreteDistributions/DistributionFilters/CloseFilters) - |
| 167 | +checks if a value continues a sequence where every value is close enough to a reference value and needs to be regenerated; |
| 168 | +- [Descendant Sequence](https://github.com/ZorPastaman/Random-Generators/tree/master/Runtime/DiscreteDistributions/DistributionFilters/DescendantSequenceFilters) - |
| 169 | +checks if a value continues a descendant sequence and it needs to be regenerated; |
| 170 | +- [Frequent Value](https://github.com/ZorPastaman/Random-Generators/tree/master/Runtime/DiscreteDistributions/DistributionFilters/FrequentValueFilters) - |
| 171 | +checks if a value is contained in a sequence more than allowed times and needs to be regenerated; |
| 172 | +- [Opposite Pattern](https://github.com/ZorPastaman/Random-Generators/tree/master/Runtime/DiscreteDistributions/DistributionFilters/OppositePatternFilters) - |
| 173 | +checks if a value forms a pattern opposite to a previous pattern and needs to be regenerated; |
| 174 | +- [Pair](https://github.com/ZorPastaman/Random-Generators/tree/master/Runtime/DiscreteDistributions/DistributionFilters/PairFilters) - |
| 175 | +Checks if a value is contained in a sequence some elements before and needs to be regenerated; |
| 176 | +- [Repeating Pattern](https://github.com/ZorPastaman/Random-Generators/tree/master/Runtime/DiscreteDistributions/DistributionFilters/RepeatingPatternFilters) - |
| 177 | +checks if a new value forms a pattern the same to a pattern some elements before and needs to be regenerated; |
| 178 | +- [Same Pattern](https://github.com/ZorPastaman/Random-Generators/tree/master/Runtime/DiscreteDistributions/DistributionFilters/SamePatternFilters) - |
| 179 | +checks if a value forms the same pattern in a sequence as a pattern before and needs to be regenerated; |
| 180 | +- [Same Sequence](https://github.com/ZorPastaman/Random-Generators/tree/master/Runtime/DiscreteDistributions/DistributionFilters/SameSequenceFilters) - |
| 181 | +checks if a value continues a sequence where every value is the same and needs to be regenerated. |
| 182 | + |
| 183 | +### References |
| 184 | + |
| 185 | +References are serializable structs that wrap an access to unique and shared generators or filters from their providers. |
| 186 | +All the references require a link to a provider. Also, they have a toggle **Shared**. |
| 187 | +If it's on, a reference returns a shared generator or filter. If it's off, a reference returns a unique generator or filter. |
| 188 | + |
| 189 | +#### List of references |
| 190 | + |
| 191 | +- [Continuous Generator Provider Reference](https://github.com/ZorPastaman/Random-Generators/blob/master/Runtime/ContinuousDistributions/ContinuousGeneratorProviderReference.cs); |
| 192 | +- [Discrete Generator Provider Reference](https://github.com/ZorPastaman/Random-Generators/blob/master/Runtime/DiscreteDistributions/DiscreteGeneratorProviderReference.cs); |
| 193 | +- [Continuous Filter Provider Reference](https://github.com/ZorPastaman/Random-Generators/blob/master/Runtime/ContinuousDistributions/DistributionFilters/ContinuousFilterProviderReference.cs); |
| 194 | +- [Discrete Filter Provider Reference](https://github.com/ZorPastaman/Random-Generators/blob/master/Runtime/DiscreteDistributions/DistributionFilters/DiscreteFilterProviderReference.cs). |
| 195 | + |
| 196 | +### Property drawers |
| 197 | + |
| 198 | +- [Require Discrete Generator](https://github.com/ZorPastaman/Random-Generators/blob/master/Runtime/PropertyDrawerAttributes/RequireDiscreteGenerator.cs) - |
| 199 | +doesn't allow to set a generator with a wrong type into |
| 200 | +[Discrete Generator Provider Reference](https://github.com/ZorPastaman/Random-Generators/blob/master/Runtime/DiscreteDistributions/DiscreteGeneratorProviderReference.cs); |
| 201 | +- [Require Discrete Filter](https://github.com/ZorPastaman/Random-Generators/blob/master/Runtime/PropertyDrawerAttributes/RequireDiscreteFilter.cs) - |
| 202 | +doesn't allow to set a filter with a wrong type into |
| 203 | +[Discrete Filter Provider Reference](https://github.com/ZorPastaman/Random-Generators/blob/master/Runtime/DiscreteDistributions/DistributionFilters/DiscreteFilterProviderReference.cs). |
0 commit comments