Git Product home page Git Product logo

random-generators's Introduction

Random Generators

Random generators for Unity is a collection of random generators, different random engines, distributions, modificators and filters.

The library has a useful infrastructure that makes it easy to expand it and add new distributions, modificators, filters and random generators.

The library is very fast and heap allocation free.

Installation

This repo is a regular Unity package. You can install it as your project dependency. More here: https://docs.unity3d.com/Manual/upm-dependencies.html.

Usage

  1. Create a provider from Assets/Create/Random Generator Providers/ or choose one of pre-made providers among DefaultBoolUniformGeneratorProvider, DefaultIntUniformGeneratorProvider, DefaultFloatUniformGeneratorProvider, DefaultNormalGeneratorProvider and DefaultBatesGeneratorProvider.
  2. Add ContinuousGeneratorProviderReference for continuous distributions or DiscreteGeneratorProviderReference for discrete distributions as a serialize field into your component.
  3. Link a selected provider into a right provider reference. Toggle on/off Shared. If Shared is on, a generator is created once and reused by all requesters. If Shared is off, a new generator is created for every requester.
  4. In your script, call ContinuousGeneratorProviderReference.generator or DiscreteGeneratorProviderReference.GetGenerator() and cache the result. They return a IContinuousGenerator or IDiscreteGenerator that generate a random value.
  5. Call Generate() in a gotten generator to get a random value that corresponds to a selected distribution.

Also, you can create your own infrastructure. Every part of this library is public and available separately from other parts.

Parts

Random Engines

Random engines are algorithms in structs that generate pseudo-random values.

List of random engines

Random Generators

Random generators use random engines (custom or pre-built in Unity) to generate pseudo-random values corresponding to a distribution. They may simply wrap random engines as well. They consist of distributions, generators and generator providers.

Distributions

Distributions are just algorithms in static classes that return a random value(s). They usually require an independent and identically distributed random generator. By default, Unity generator is used as such a generator. Also, the distributions support Func<float> and IContinuousGenerator as an iid random generator.

Generators

Generators are standard c# classes that implement IContinuousGenerator or IDiscreteGenerator and wrap one of the methods of the distributions.

Generator Providers

Generator providers are scriptable objects and can be linked to a serialize field in Unity components. They wrap generators and provide unique and shared instances of them.

List of continuous generator algorithms
List of discrete generator algorithms

Random modificators

There are different modificators in this library. They modify results of generators and mimic them. Modificators has providers as random generators.

Modificators

Modificators are standard c# classes that implement IContinuousGenerator or IDiscreteGenerator but they are not actually generators, they take a generated value from a depended generator, modify it somehow and return a result.

Modificator Providers

Modificator providers are scriptable objects and can be linked to a serialize field in Unity components. They wrap modificators and provide unique and shared instances of them.

List of continuous modificators
  • Add - sums a generated value and a multiplier and returns the result;
  • Clamp - clamps a generated value between specified minimum and maximum values;
  • Multiply - multiplies a generated value and an item and returns the result;
  • Round - rounds a generated value to a nearest integer.
List of discrete modificators
  • Add - sums a generated value and a multiplier and returns the result;
  • Clamp - clamps a generated value between specified minimum and maximum values;
  • Round to Int - rounds a generated value to a nearest integer.

Random filters

For usual people random values may look like non-random. Because of that we need to filter results of random generators and regenerate them if a filter forbids a new value.

Filters

Filters are algorithms in static classes that check if a new generated value corresponds to their rules. They usually forbid certain sequences of random generated values.

Filter Wrappers

Filter wrappers are standard c# classes that implement IContinuousFilter or IDiscreteFilter and wrap one of the methods of filters.

Filter Providers

Filter providers are scriptable objects and can be linked to a serialize field in Unity components. They wrap filter wrappers and provide unique and shared instances of them.

Filtered Generators

Filtered generators are standard c# classes that implement IContinuousFilter or IDiscreteFilter. They take a generated value from a depended generator and check that value with filters. If at least one filter doesn't approve a new value, it's regenerated and checked again.

Filtered Generator Providers

Filtered generator providers are scriptable objects and can be linked to a serialize field in Unity components. They wrap filtered generators and provide unique and shared instances of them.

List of continuous filters
  • Ascendant Sequence - checks if a value continues an ascendant sequence and it needs to be regenerated;
  • Close - checks if a value continues a sequence where every value is close enough to a reference value and needs to be regenerated;
  • Descendant Sequence - checks if a value continues a descendant sequence and it needs to be regenerated;
  • Greater - checks if a value continues a sequence where every value is greater than a reference value and needs to be regenerated;
  • In Range - checks if a value continues a sequence where every value is in range between the minimum and maximum and needs to be regenerated;
  • Less - checks if a value continues a sequence where every value is less than a reference value and needs to be regenerated;
  • Little Difference - checks if a value continues a sequence where consecutive elements differ by less than a required difference and needs to be regenerated;
  • Not In Range - checks if a value continues a sequence where every value is in range between the minimum and maximum and needs to be regenerated.
List of discrete filters
  • Ascendant Sequence - checks if a value continues an ascendant sequence and it needs to be regenerated;
  • Close - checks if a value continues a sequence where every value is close enough to a reference value and needs to be regenerated;
  • Descendant Sequence - checks if a value continues a descendant sequence and it needs to be regenerated;
  • Frequent Value - checks if a value is contained in a sequence more than allowed times and needs to be regenerated;
  • Opposite Pattern - checks if a value forms a pattern opposite to a previous pattern and needs to be regenerated;
  • Pair - Checks if a value is contained in a sequence some elements before and needs to be regenerated;
  • Repeating Pattern - checks if a new value forms a pattern the same to a pattern some elements before and needs to be regenerated;
  • Same Pattern - checks if a value forms the same pattern in a sequence as a pattern before and needs to be regenerated;
  • Same Sequence - checks if a value continues a sequence where every value is the same and needs to be regenerated.

References

References are serializable structs that wrap an access to unique and shared generators or filters from their providers. All the references require a link to a provider. Also, they have a toggle Shared. If it's on, a reference returns a shared generator or filter. If it's off, a reference returns a unique generator or filter.

List of references

Property drawers

Distribution Tests

In Window/Random Generators/ there are different distribution tests where you can test any distribution asset and see probabilities of its values.

random-generators's People

Contributors

zorpastaman avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar

random-generators's Issues

Here's my use case, how would you suggest I apply your library to it?

I opened #1 with a clear idea of my needs, but not of how they relate to this package. So I'll describe what I'm trying to do, and I'd appreciate your insight in how your library can be used for it.

I'm making a falling-block puzzle game (think Tetris or Puyo Puyo or Columns) with multiplayer. Each block has a different shape, and each shape in turn is made of pieces from a fixed set of colors. These shapes and colors are represented in my game as ScriptableObjects.

The set of possible shapes and colors expands at fixed intervals; this is currently done by replacing the active probability distribution with a new preset one (as opposed to modifying it in-place).

The shapes and colors are determined randomly, but in multiplayer mode both players must get the same sequence of pieces (including color and shape). It's still random, but both players get the same seed at the start of the game and both RNGs are running at the same rate.

It's okay if colors and shapes use different RNGs, or even different seeds; however, both players must get the same set of seeds at the start of the game.

I have a gut feeling that Random-Generators will better fit this use case than my current implementation, but I don't know what that would look like in practice. What would you suggest?

[Bug] WeightedGeneratorProvider and WeightedGeneratorDependentProvider don't show their weights and values when Naughty Attributes is installed

Describe the bug
When NaughtyAttributes and Random-Generators are both installed in a project, the inspector for WeightedGeneratorProvider<T>s and WeightedGeneratorDependentProvider<T>s do not draw the weights and values of the provider's distribution.

To Reproduce

  1. Install Random-Generators.
  2. Create a WeightedGeneratorProvider<T> or WeightedGeneratorDependentProvider<T> from the Assets menu. In my case, I used IntWeightedGeneratorDependentProvider, available through Assets/Create/Random Generator Providers/Discrete Distributions/Weighted Distribution/Int Weighted Generator Dependent Provider.
  3. Populate the created distribution provider with your favorite values.
  4. Ensure it's saved.
  5. Install NaughtyAttributes.
  6. Open the previously-created distribution in the inspector.
  7. The values will not be visible or editable, so you won't be able to adjust them. (They will still be serialized.)

Expected behavior
This:

Screenshot 2021-07-21 165600

Actual behavior
This:

Screenshot 2021-07-21 165801

Unity Editor:

  • OS: Windows 10
  • Unity version: 2021.1.15f1
  • Package version: Random-Generators 1.3.0, Naughty Attributes 2.1.0

Additional context

  • This issue occurs with the built-in weighted generator providers and with subclasses.
  • Other generator providers are not affected.
  • I don't know if this is a bug in Random-Generators or in NaughtyAttributes, so I'm reporting the issue in both projects.
  • I initially believed that Odin Inspector was the culprit, but I was able to reproduce this issue in a blank project that did not have Odin installed.
  • Nothing is logged and no exceptions are thrown. The custom editor simply fails silently.

[Feature] Document some possible uses of each distribution or other feature (especially for those who don't know the math too well)

Is your feature request related to a problem? Please describe.
I find probability and statistics confusing and unintuitive; This library is easy, it's just a thin wrapper over ideas that mostly aren't. I'm already getting a lot out of some of the simpler concepts (like weighted distributions), but I bet that someone with a deep understanding of the math would get a lot out of this library.

I assume you have this deep understanding, since you wrote the thing. In college I got a C in probability.

Describe the solution you'd like
Documentation for each distribution (or other interesting aspect of this library) that outlines some game-related scenarios in which it would be useful. Doesn't need to be exhaustive, a couple of sentences would suffice.

Describe alternatives you've considered
A probability textbook.

Additional context
I'm assuming that you're developing this library for use in your own projects? In that case, simply writing about the things you're using this library for would go a long way.

The main impetus for me opening this issue is that I'm trying to model a particular event in my game but don't know which distribution to use. (The details probably deserve a separate thread.)

Ability to set a seed (or maybe a System.Random instance)?

Hi there. I'm strongly considering using this library for probability distributions in my own game. However, I need to be able to set the initial seed for all distributions; I can't rely on UnityEngine.Random except for that initial seed. Is it possible to explicitly provide a seed (or a System.Random instance) for each generator to use?

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.