CabConModding
Facebook
Twitter
youtube
Discord
Contact us
RSS
Menu
CabConModding
Home
New
Top
Premium
Rules
FAQ - Frequently Asked Questions
Games
Fornite
Call of Duty: Black Ops 3
Clash of Clans
Grand Theft Auto 5
Apex Legends
Assassin’s Creed Origins
Forums
Premium
Latest posts
What's new
Latest posts
New profile posts
Latest activity
Members
Current visitors
New profile posts
Log in
Register
What's new
Premium
Latest posts
Menu
Log in
Register
Navigation
Install the app
Install
More options
Dark Theme
Contact us
Close Menu
Forums
Tech Boards
Computer Programming
Source Code & Tutorial
Custom Random Generation Class
JavaScript is disabled. For a better experience, please enable JavaScript in your browser before proceeding.
You are using an out of date browser. It may not display this or other websites correctly.
You should upgrade or use an
alternative browser
.
Reply to thread
Message
<blockquote data-quote="zerker24" data-source="post: 16087" data-attributes="member: 28218"><p>Okay, this is a little pointless lol. It was a project of mine to help myself learn a little more, and I wanted to see what you guys think. Basically it works the same as the build in random class in about EVERY language, but I looked up the algorithms and wrote everything out myself. There is a few goodies in here as well.</p><p></p><p>Features:</p><ul> <li data-xf-list-type="ul">Generate a pseudo-randomized value.</li> <li data-xf-list-type="ul">Generate a pseudo-randomized decimal (between 0 - 1) value.</li> <li data-xf-list-type="ul">Generate a pseudo-randomized value between a Minimum and Maximum.</li> <li data-xf-list-type="ul">Shuffle any array using the Fisher-Yates shuffle.</li> <li data-xf-list-type="ul">Shuffle any List<T> with the Fisher-Yates shuffle.</li> <li data-xf-list-type="ul">Generate an array of random values.</li> <li data-xf-list-type="ul">Generate a List<T> of random value.</li> </ul><p></p><p>[code]</p><p>using System;</p><p>using System.Collections;</p><p>using System.Collections.Generic;</p><p></p><p>namespace Random_Generator</p><p>{</p><p> public class ZerkRandom</p><p> {</p><p> private int seed = 0;</p><p> private double lastVal = 0;</p><p> // A, M, and C based on Java's java.util.Random</p><p> private double a = 25214903917;</p><p> private double m = Math.Pow(2, 48);</p><p> private double c = 11;</p><p></p><p> public ZerkRandom(int seed)</p><p> {</p><p> this.seed = seed;</p><p> lastVal = seed;</p><p> }</p><p></p><p> private List<double> FindFactors(double num)</p><p> {</p><p> List<double> result = new List<double>();</p><p></p><p> // Take out the 2s.</p><p> while (num % 2 == 0)</p><p> {</p><p> result.Add(2);</p><p> num /= 2;</p><p> }</p><p></p><p> // Take out other primes.</p><p> long factor = 3;</p><p> while (factor * factor <= num)</p><p> {</p><p> if (num % factor == 0)</p><p> {</p><p> // This is a factor.</p><p> result.Add(factor);</p><p> num /= factor;</p><p> }</p><p> else</p><p> {</p><p> // Go to the next odd number.</p><p> factor += 2;</p><p> }</p><p> }</p><p></p><p> // If num is not 1, then whatever is left is prime.</p><p> if (num > 1) result.Add(num);</p><p></p><p> return result;</p><p> }</p><p></p><p> public void SetAMC(double a, double m, double c)</p><p> {</p><p> double tmpC = c;</p><p> double tmpM = m;</p><p> // Check if C and M are co-prime as per rule 1;</p><p> while (tmpC != 0 && tmpM != 0)</p><p> {</p><p> if (tmpC > tmpM)</p><p> tmpC %= tmpM;</p><p> else</p><p> tmpM %= tmpC;</p><p> }</p><p> if (Math.Max(tmpC, tmpM) != 1)</p><p> throw new Exception("C and M must be relatively prime or co-prime.");</p><p></p><p> //Get value of a - 1 for the next two rules</p><p> double aTmp = a - 1;</p><p></p><p> //Make sure aTmp is divisable by the prime factors of m</p><p> List<double> primes = FindFactors(m);</p><p> bool isPrime = true;</p><p></p><p> foreach (double d in primes)</p><p> {</p><p> if ((aTmp % d) != 0)</p><p> {</p><p> isPrime = false;</p><p> return;</p><p> }</p><p> }</p><p></p><p> if (!isPrime)</p><p> throw new Exception("A - 1 must be divisible by all the prime factors of M.");</p><p></p><p> //If m is a multiple of 4, aTmp must be also.</p><p> bool isMultiple = m % 4 == 0;</p><p> if (isMultiple)</p><p> {</p><p> bool aIsMultiple = aTmp % 4 == 0;</p><p> if (!aIsMultiple)</p><p> throw new Exception("If M is a multiple of 4, A - 1 must be also. ");</p><p> }</p><p></p><p> this.a = a;</p><p> this.m = m;</p><p> this.c = c;</p><p> }</p><p></p><p> /// <summary></p><p> /// Gets gets an array of the bits in the value.</p><p> /// </summary></p><p> /// <param name="value">The value to convert.</param></p><p> /// <returns>a byte array of bits.</returns></p><p> private byte[] GetBits(double value)</p><p> {</p><p> byte[] bytes = BitConverter.GetBytes(value);</p><p> BitArray bitArray = new BitArray(bytes);</p><p> List<byte> bits = new List<byte>();</p><p> foreach (bool b in bitArray)</p><p> bits.Add(Convert.ToByte(b));</p><p> return bits.ToArray();</p><p> }</p><p></p><p> /// <summary></p><p> /// Takes a set number of bits, and turns them into a int32.</p><p> /// </summary></p><p> /// <param name="bits">The bits to start from.</param></p><p> /// <param name="startingBit">The bit to start from. Should be more than 32 bits away from the end.</param></p><p> /// <param name="endingBit">The bit to end on. Should be no more than 32 bits from the start.</param></p><p> /// <returns></returns></p><p> private int GetValue(byte[] bits, int startingBit, int endingBit)</p><p> {</p><p> string bitString = "";</p><p> for (int i = startingBit; i < endingBit; i++)</p><p> bitString += bits[i];</p><p> return Convert.ToInt32(bitString, 2);</p><p> }</p><p></p><p> /// <summary></p><p> /// Generates a random number using a linear congruential generator.</p><p> /// </summary></p><p> /// <returns>A double.</returns></p><p> public double Generate()</p><p> {</p><p> // Standard Linear Congruential Generator equation.</p><p> double value = (a * lastVal + c) % m;</p><p> // This next part just helps the program be more random.</p><p> int rtnInt = GetValue(GetBits(value), 10, 42);</p><p> lastVal = rtnInt;</p><p> return rtnInt;</p><p> }</p><p> /// <summary></p><p> /// Generates a random number using a linear congruential generator.</p><p> /// </summary></p><p> /// <returns>A double.</returns></p><p> public double Generate(bool PosativeOnly)</p><p> {</p><p> // Standard Linear Congruential Generator equation.</p><p> double value = (a * lastVal + c) % m;</p><p> // This next part just helps the program be more random.</p><p> int rtnInt = GetValue(GetBits(value), 10, 42);</p><p> lastVal = rtnInt;</p><p> if (PosativeOnly)</p><p> rtnInt = Math.Abs(rtnInt);</p><p> return rtnInt;</p><p> }</p><p> /// <summary></p><p> /// Generates a random number using a linear congruential generator.</p><p> /// </summary></p><p> /// <returns>A double between 0 and 1.</returns></p><p> public double GenerateDecimal()</p><p> {</p><p></p><p> double value = Generate(true);</p><p> lastVal = value;</p><p> value = value % 100;</p><p> return value / 100;</p><p> }</p><p> /// <summary></p><p> /// Generates a random number using a linear congruential generator.</p><p> /// </summary></p><p> /// <param name="min">The minimum number the generator will generate.</param></p><p> /// <param name="max">The highest number the generator will generate.</param></p><p> /// <returns>A double between the values of min and max.</returns></p><p> public double Generate(double min, double max)</p><p> {</p><p> double diffRate = max - min;</p><p> double value = 0;</p><p> if (min >= 0)</p><p> value = Generate(true);</p><p> else</p><p> value = Generate();</p><p> lastVal = value;</p><p> return (value % diffRate) + min;</p><p> }</p><p></p><p> /// <summary></p><p> /// Shuffles a givin array randomly using the Fisher-Yates Shuffle.</p><p> /// </summary></p><p> /// <typeparam name="T">The arrays object type.</typeparam></p><p> /// <param name="array">The array to shuffle.</param></p><p> /// <returns>A shuffled version of the supplied array.</returns></p><p> public T[] ShuffleArray<T>(T[] array)</p><p> {</p><p> T[] rtnArray = new T[array.Length];</p><p> Array.Copy(array, rtnArray, array.Length);</p><p> int n = rtnArray.Length;</p><p> for (int i = 0; i < n; i++)</p><p> {</p><p> int r = i + (int)(GenerateDecimal() * (n - i));</p><p> T t = rtnArray[r];</p><p> rtnArray[r] = rtnArray[i];</p><p> rtnArray[i] = t;</p><p> }</p><p> return rtnArray;</p><p> }</p><p> /// <summary></p><p> /// Shuffles a givin list randomly using the Fisher-Yates Shuffle.</p><p> /// </summary></p><p> /// <typeparam name="T">The arrays object type.</typeparam></p><p> /// <param name="list">The list to shuffle.</param></p><p> /// <returns>A shuffled version of the supplied list.</returns></p><p> public List<T> ShuffleList<T>(List<T> list)</p><p> {</p><p> List<T> rtnList = new List<T>();</p><p> T[] array = list.ToArray();</p><p> int n = array.Length;</p><p> for (int i = 0; i < n; i++)</p><p> {</p><p> int r = i + (int)(GenerateDecimal() * (n - i));</p><p> T t = array[r];</p><p> array[r] = array[i];</p><p> array[i] = t;</p><p> }</p><p> foreach (T t in array)</p><p> rtnList.Add(t);</p><p> return rtnList;</p><p> }</p><p></p><p> /// <summary></p><p> /// Generates an array of values.</p><p> /// </summary></p><p> /// <param name="length">The length of the return array.</param></p><p> /// <returns>An array of random double values.</returns></p><p> public double[] GenerateArray(int length)</p><p> {</p><p> List<double> data = new List<double>();</p><p> for (int i = 0; i < length; i++)</p><p> data.Add(this.Generate());</p><p> return data.ToArray();</p><p> }</p><p> /// <summary></p><p> /// Generates an array of values.</p><p> /// </summary></p><p> /// <param name="length">The length of the return array.</param></p><p> /// <param name="min">The min value the system can generate.</param></p><p> /// <param name="max">The max value the system can generate.</param></p><p> /// <returns>An array of random double values.</returns></p><p> public double[] GenerateArray(int length, int min, int max)</p><p> {</p><p> List<double> data = new List<double>();</p><p> for (int i = 0; i < length; i++)</p><p> data.Add(this.Generate(min, max));</p><p> return data.ToArray();</p><p> }</p><p> /// <summary></p><p> /// Generates an array of values.</p><p> /// </summary></p><p> /// <param name="length">The length of the return array.</param></p><p> /// <returns>An array of random double values between 0 and 1.</returns></p><p> public double[] GenerateDecimalArray(int length)</p><p> {</p><p> List<double> data = new List<double>();</p><p> for (int i = 0; i < length; i++)</p><p> data.Add(this.GenerateDecimal());</p><p> return data.ToArray();</p><p> }</p><p></p><p> /// <summary></p><p> /// Generates an list of values.</p><p> /// </summary></p><p> /// <param name="length">The length of the return list.</param></p><p> /// <returns>An array of random double values.</returns></p><p> public List<double> GenerateList(int length)</p><p> {</p><p> List<double> data = new List<double>();</p><p> for (int i = 0; i < length; i++)</p><p> data.Add(this.Generate());</p><p> return data;</p><p> }</p><p> /// <summary></p><p> /// Generates an list of values.</p><p> /// </summary></p><p> /// <param name="length">The length of the return list.</param></p><p> /// <param name="min">The min value the system can generate.</param></p><p> /// <param name="max">The max value the system can generate.</param></p><p> /// <returns>An list of random double values.</returns></p><p> public List<double> GenerateList(int length, int min, int max)</p><p> {</p><p> List<double> data = new List<double>();</p><p> for (int i = 0; i < length; i++)</p><p> data.Add(this.Generate(min, max));</p><p> return data;</p><p> }</p><p> /// <summary></p><p> /// Generates an list of values.</p><p> /// </summary></p><p> /// <param name="length">The length of the return list.</param></p><p> /// <returns>An list of random double values between 0 and 1.</returns></p><p> public List<double> GenerateDecimalList(int length)</p><p> {</p><p> List<double> data = new List<double>();</p><p> for (int i = 0; i < length; i++)</p><p> data.Add(this.GenerateDecimal());</p><p> return data;</p><p> }</p><p> }</p><p>}</p><p>[/code]</p></blockquote><p></p>
[QUOTE="zerker24, post: 16087, member: 28218"] Okay, this is a little pointless lol. It was a project of mine to help myself learn a little more, and I wanted to see what you guys think. Basically it works the same as the build in random class in about EVERY language, but I looked up the algorithms and wrote everything out myself. There is a few goodies in here as well. Features: [LIST] [*]Generate a pseudo-randomized value. [*]Generate a pseudo-randomized decimal (between 0 - 1) value. [*]Generate a pseudo-randomized value between a Minimum and Maximum. [*]Shuffle any array using the Fisher-Yates shuffle. [*]Shuffle any List<T> with the Fisher-Yates shuffle. [*]Generate an array of random values. [*]Generate a List<T> of random value. [/LIST] [code] using System; using System.Collections; using System.Collections.Generic; namespace Random_Generator { public class ZerkRandom { private int seed = 0; private double lastVal = 0; // A, M, and C based on Java's java.util.Random private double a = 25214903917; private double m = Math.Pow(2, 48); private double c = 11; public ZerkRandom(int seed) { this.seed = seed; lastVal = seed; } private List<double> FindFactors(double num) { List<double> result = new List<double>(); // Take out the 2s. while (num % 2 == 0) { result.Add(2); num /= 2; } // Take out other primes. long factor = 3; while (factor * factor <= num) { if (num % factor == 0) { // This is a factor. result.Add(factor); num /= factor; } else { // Go to the next odd number. factor += 2; } } // If num is not 1, then whatever is left is prime. if (num > 1) result.Add(num); return result; } public void SetAMC(double a, double m, double c) { double tmpC = c; double tmpM = m; // Check if C and M are co-prime as per rule 1; while (tmpC != 0 && tmpM != 0) { if (tmpC > tmpM) tmpC %= tmpM; else tmpM %= tmpC; } if (Math.Max(tmpC, tmpM) != 1) throw new Exception("C and M must be relatively prime or co-prime."); //Get value of a - 1 for the next two rules double aTmp = a - 1; //Make sure aTmp is divisable by the prime factors of m List<double> primes = FindFactors(m); bool isPrime = true; foreach (double d in primes) { if ((aTmp % d) != 0) { isPrime = false; return; } } if (!isPrime) throw new Exception("A - 1 must be divisible by all the prime factors of M."); //If m is a multiple of 4, aTmp must be also. bool isMultiple = m % 4 == 0; if (isMultiple) { bool aIsMultiple = aTmp % 4 == 0; if (!aIsMultiple) throw new Exception("If M is a multiple of 4, A - 1 must be also. "); } this.a = a; this.m = m; this.c = c; } /// <summary> /// Gets gets an array of the bits in the value. /// </summary> /// <param name="value">The value to convert.</param> /// <returns>a byte array of bits.</returns> private byte[] GetBits(double value) { byte[] bytes = BitConverter.GetBytes(value); BitArray bitArray = new BitArray(bytes); List<byte> bits = new List<byte>(); foreach (bool b in bitArray) bits.Add(Convert.ToByte(b)); return bits.ToArray(); } /// <summary> /// Takes a set number of bits, and turns them into a int32. /// </summary> /// <param name="bits">The bits to start from.</param> /// <param name="startingBit">The bit to start from. Should be more than 32 bits away from the end.</param> /// <param name="endingBit">The bit to end on. Should be no more than 32 bits from the start.</param> /// <returns></returns> private int GetValue(byte[] bits, int startingBit, int endingBit) { string bitString = ""; for (int i = startingBit; i < endingBit; i++) bitString += bits[i]; return Convert.ToInt32(bitString, 2); } /// <summary> /// Generates a random number using a linear congruential generator. /// </summary> /// <returns>A double.</returns> public double Generate() { // Standard Linear Congruential Generator equation. double value = (a * lastVal + c) % m; // This next part just helps the program be more random. int rtnInt = GetValue(GetBits(value), 10, 42); lastVal = rtnInt; return rtnInt; } /// <summary> /// Generates a random number using a linear congruential generator. /// </summary> /// <returns>A double.</returns> public double Generate(bool PosativeOnly) { // Standard Linear Congruential Generator equation. double value = (a * lastVal + c) % m; // This next part just helps the program be more random. int rtnInt = GetValue(GetBits(value), 10, 42); lastVal = rtnInt; if (PosativeOnly) rtnInt = Math.Abs(rtnInt); return rtnInt; } /// <summary> /// Generates a random number using a linear congruential generator. /// </summary> /// <returns>A double between 0 and 1.</returns> public double GenerateDecimal() { double value = Generate(true); lastVal = value; value = value % 100; return value / 100; } /// <summary> /// Generates a random number using a linear congruential generator. /// </summary> /// <param name="min">The minimum number the generator will generate.</param> /// <param name="max">The highest number the generator will generate.</param> /// <returns>A double between the values of min and max.</returns> public double Generate(double min, double max) { double diffRate = max - min; double value = 0; if (min >= 0) value = Generate(true); else value = Generate(); lastVal = value; return (value % diffRate) + min; } /// <summary> /// Shuffles a givin array randomly using the Fisher-Yates Shuffle. /// </summary> /// <typeparam name="T">The arrays object type.</typeparam> /// <param name="array">The array to shuffle.</param> /// <returns>A shuffled version of the supplied array.</returns> public T[] ShuffleArray<T>(T[] array) { T[] rtnArray = new T[array.Length]; Array.Copy(array, rtnArray, array.Length); int n = rtnArray.Length; for (int i = 0; i < n; i++) { int r = i + (int)(GenerateDecimal() * (n - i)); T t = rtnArray[r]; rtnArray[r] = rtnArray[i]; rtnArray[i] = t; } return rtnArray; } /// <summary> /// Shuffles a givin list randomly using the Fisher-Yates Shuffle. /// </summary> /// <typeparam name="T">The arrays object type.</typeparam> /// <param name="list">The list to shuffle.</param> /// <returns>A shuffled version of the supplied list.</returns> public List<T> ShuffleList<T>(List<T> list) { List<T> rtnList = new List<T>(); T[] array = list.ToArray(); int n = array.Length; for (int i = 0; i < n; i++) { int r = i + (int)(GenerateDecimal() * (n - i)); T t = array[r]; array[r] = array[i]; array[i] = t; } foreach (T t in array) rtnList.Add(t); return rtnList; } /// <summary> /// Generates an array of values. /// </summary> /// <param name="length">The length of the return array.</param> /// <returns>An array of random double values.</returns> public double[] GenerateArray(int length) { List<double> data = new List<double>(); for (int i = 0; i < length; i++) data.Add(this.Generate()); return data.ToArray(); } /// <summary> /// Generates an array of values. /// </summary> /// <param name="length">The length of the return array.</param> /// <param name="min">The min value the system can generate.</param> /// <param name="max">The max value the system can generate.</param> /// <returns>An array of random double values.</returns> public double[] GenerateArray(int length, int min, int max) { List<double> data = new List<double>(); for (int i = 0; i < length; i++) data.Add(this.Generate(min, max)); return data.ToArray(); } /// <summary> /// Generates an array of values. /// </summary> /// <param name="length">The length of the return array.</param> /// <returns>An array of random double values between 0 and 1.</returns> public double[] GenerateDecimalArray(int length) { List<double> data = new List<double>(); for (int i = 0; i < length; i++) data.Add(this.GenerateDecimal()); return data.ToArray(); } /// <summary> /// Generates an list of values. /// </summary> /// <param name="length">The length of the return list.</param> /// <returns>An array of random double values.</returns> public List<double> GenerateList(int length) { List<double> data = new List<double>(); for (int i = 0; i < length; i++) data.Add(this.Generate()); return data; } /// <summary> /// Generates an list of values. /// </summary> /// <param name="length">The length of the return list.</param> /// <param name="min">The min value the system can generate.</param> /// <param name="max">The max value the system can generate.</param> /// <returns>An list of random double values.</returns> public List<double> GenerateList(int length, int min, int max) { List<double> data = new List<double>(); for (int i = 0; i < length; i++) data.Add(this.Generate(min, max)); return data; } /// <summary> /// Generates an list of values. /// </summary> /// <param name="length">The length of the return list.</param> /// <returns>An list of random double values between 0 and 1.</returns> public List<double> GenerateDecimalList(int length) { List<double> data = new List<double>(); for (int i = 0; i < length; i++) data.Add(this.GenerateDecimal()); return data; } } } [/code] [/QUOTE]
Verification
Post reply
Forums
Tech Boards
Computer Programming
Source Code & Tutorial
Custom Random Generation Class
This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
By continuing to use this site, you are consenting to our use of cookies.
Accept
Learn more…
Top