Homepage: http://tucanosoftware.com/projects/unityrandom
Example: http://108.166.95.149/UnityTest/UnityRandomExample/UnityRandomExample.html
In Unity3d there is already a Random number generator based on the platform-specific random generator.
Here we present an alternative Random library for Unity3d designed to generate uniform Pseudo-Random deviates.
The library use a fast PRNG (Mersenne-Twister) to generate: Floating Numbers in range [0-1] and in range [n-m], Vector2 and Vector3 data types.
The library comes with some special functions designed specifically for a game design framework: Shuffle Bag, Dice, Random Color.
The uniform deviates can be transformed with the distributions: Standard Normal Distribution and Power-Law. In addition is possible to generate floating random deviates coming from other distributions: Poisson, Exponential and Gamma.
How I can test the random numbers?The library add a window to the Unity3D editor that allow you to Test and Visualize your random numbers, Vector2 and Vector3. With the SAVE button, you can write the sample of random number to a txt file. This is useful if you need to analyze in deep the distribution of your random numbers with a statistical software.
Initialization
Initialization with a seed: UnityRandom urand = new UnityRandom(int seed);
Numbers
A Random number in range [0-1]: @float val = urand.Value()
Transformations
A Random number in range [0-1] with a Transformation: float val = urand.Value(UnityRandom.Normalization.STDNORMAL, 5.0f)
Vectors
A random point in a disk with R=1: Vector2 pos = urand.PointInADisk()
Colors
A random color in the range of visible light (rainbow): Color col = urand.Rainbow()
Dice
A 2D6 dice roll DiceRoll roll = urand.RollDice(2,DiceRoll.DiceType.D6)
Shuffle Bag
In games or educational programs you often don’t want real randomness. Unsurprisingly random it’s often a bit too random. You may get some items several times in a row and others too rarely. It’s either too easy or too hard. Balancing the weights is hard as well, because each run is too different. And if that wouldn’t be already bad enough; it’s also difficult to verify the results.
A shuffle Bag with values: [1,10]
float[] shufflebag = {1,2,3,4,5,6,7,8,9,10};
ShuffleBagCollection<float> thebag = _urand.ShuffleBag(shufflebag);
float randvalue = thebag.Next()
A shuffle bag with weighted values:
Dictionary wshufflebag = new Dictionary<float,int>();
wshufflebag[1] = 5;
wshufflebag[2] = 45;
wshufflebag[3] = 25;
wshufflebag[4] = 25;
@ShuffleBagCollection thebag = _urand.ShuffleBag(wshufflebag); @
float randvalue = thebag.Next()
UnityRandom urand = new UnityRandom();
UnityRandom urand = new UnityRandom(int seed);
Generation of uniform deviates in any range.
Available Transformations
UnityRandom.Normalization.STDNORMAL
with parameter: float temperature
UnityRandom.Normalization.POWERLAW
with parameter: float power
float val = urand.Value()
float val = urand.Value(UnityRandom.Normalization.STDNORMAL, 5.0f)
float val = urand.Range(1,100)
float val = urand.Value(0,100,UnityRandom.Normalization.POWERLAW, 5.0f)
float val = urand.Poisson(5.0f)
float val = urand.Exponential(5.0f)
float val = urand.Gamma(5.0f)
generation of Unity Vector2 Objects.
Vector2 pos = urand.PointInASquare()
Vector2 pos = urand.PointInASquare(UnityRandom.Normalization.STDNORMAL, 5.0f)
Vector2 pos = urand.PointInACircle()
Vector2 pos = urand.PointInACircle(UnityRandom.Normalization.STDNORMAL, 5.0f)
Vector2 pos = urand.PointInADisk()
Vector2 pos = urand.PointInADisk(UnityRandom.Normalization.STDNORMAL, 5.0f)
generation of Unity Vector3
Vector3 pos = urand.PointInACube()
Vector3 pos = urand.PointInACube(UnityRandom.Normalization.STDNORMAL, 5.0f)
Vector3 pos = urand.PointOnACube()
Vector3 pos = urand.PointOnACube(UnityRandom.Normalization.STDNORMAL, 5.0f)
Vector3 pos = urand.PointInASphere()
Vector3 pos = urand.PointOnASphere()
Color col = urand.Rainbow()
Color col = urand.Rainbow(UnityRandom.Normalization.STDNORMAL, 5.0f)
DiceRoll roll = urand.RollDice(n,type)
Dice types:
Example 2D6:
DiceRoll roll = urand.RollDice(2,DiceRoll.DiceType.D6)
From: http://kaioa.com/node/53
In games or educational programs you often don’t want real randomness. Unsurprisingly random it’s often a bit too random. You may get some items several times in a row and others too rarely. It’s either too easy or too hard. Balancing the weights is hard as well, because each run is too different. And if that wouldn’t be already bad enough; it’s also difficult to verify the results.
A shuffle Bag with values: [1,10]
float[] shufflebag = {1,2,3,4,5,6,7,8,9,10};
ShuffleBagCollection<float> thebag = _urand.ShuffleBag(shufflebag);
float randvalue = thebag.Next()
A shuffle bag with weighted values:
Dictionary wshufflebag = new Dictionary<float,int>();
wshufflebag[1] = 5;
wshufflebag[2] = 45;
wshufflebag[3] = 25;
wshufflebag[4] = 25;
@ShuffleBagCollection thebag = _urand.ShuffleBag(wshufflebag); @
float randvalue = thebag.Next()
RetroSearch is an open source project built by @garambo | Open a GitHub Issue
Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo
HTML:
3.2
| Encoding:
UTF-8
| Version:
0.7.4