ExtRandom

Article Original Author: Dreamblur

ExtRandom.cs v.1.0 Original Script Author: Dreamblur

Description
ExtRandom is an extension of the Unity class Random. Its main purpose is to automate the common operations which implement the Random class.

Usage
Place the ExtRandom script in your project's Standard Assets folder.

Methods

 * RandomizeSeed
 * SplitChance
 * Chance(int, int)
 * Choice(T[])
 * Choice(List)
 * WeightedChoice(T[], int[])
 * WeightedChoice(List, int[])
 * Shuffle(T[])
 * Shuffle(List)

ExtRandom.RandomizeSeed

This method sets a random seed for the RNG using 2 convoluted formulas. Using this method at any time other than during downtime is not recommended. This method will never see use 99.9% of the time, but is available in cases where a truly random seed is required.

ExtRandom.SplitChance

This method returns either true or false with an equal chance.

ExtRandom.Chance(int, int)

This method returns either true or false with the chance of the former derived from the parameters passed to the method.

The above code has a 10% chance of returning true.

ExtRandom.Choice(T[])

This method returns a random element chosen from an array of elements.

The above code has a 33.3% chance of returning "sun", a 33.3% chance of returning "moon" and a 33.3% chance of returning "star".

ExtRandom.Choice(List)

This method returns a random element chosen from a list of elements.

The above code has a 33.3% chance of returning "sun", a 33.3% chance of returning "moon" and a 33.3% chance of returning "star".

ExtRandom.WeightedChoice(T[], int[])

This method returns a random element chosen from an array of elements based on the respective weights of the elements.

The above code has a 10% chance of returning "sun", a 25% chance of returning "moon" and a 65% chance of returning "star".

ExtRandom.WeightedChoice(List, int[])

This method returns a random element chosen from a list of elements based on the respective weights of the elements.

The above code has a 10% chance of returning "sun", a 25% chance of returning "moon" and a 65% chance of returning "star".

ExtRandom.Shuffle(T[])

This method rearranges the elements of an array randomly and returns the rearranged array.

The above code will shuffle the elements of strArray and return the shuffled array.

ExtRandom.Shuffle(List)

This method rearranges the elements of a list randomly and returns the rearranged list.

The above code will shuffle the elements of strList and return the shuffled list.

Testing
The following script can be used to test the statistical accuracy of the methods of the ExtRandom class. This script is not required to use ExtRandom.