Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- public class RandomExtension : Random
- {
- int Seed { get; set; }
- public RandomExtension(int seed)
- {
- Seed = seed;
- }
- public double NextDouble(double minValue, double maxValue)
- {
- if (maxValue <= minValue) throw new ArgumentException("minValue cannot be greater than maxValue");
- Random gnr = new Random(Seed);
- return gnr.NextDouble() * (maxValue - minValue) + minValue;
- }
- private static int NextValue(int number, bool isEven, int minValue, int maxValue)
- {
- Random gnr = new Random(number);
- int rndNum = gnr.Next(minValue, maxValue);
- if (rndNum % 2 == 0)
- {
- if (isEven) return rndNum;
- if (rndNum + 1 < maxValue) return rndNum + 1;
- if (rndNum - 1 > 0) return rndNum - 1;
- throw new ArgumentException("no numbers in this range");
- }
- if (!isEven) return rndNum;
- if (rndNum + 1 < maxValue) return rndNum + 1;
- if (rndNum - 1 > 0) return rndNum - 1;
- throw new ArgumentException("no numbers in this range");
- }
- public int NextEven(int minValue, int maxValue)
- {
- if (maxValue <= minValue) throw new ArgumentException("minValue cannot be greater than maxValue");
- return NextValue(Seed, true, minValue, maxValue);
- }
- public int NextOdd(int minValue, int maxValue)
- {
- if (maxValue <= minValue) throw new ArgumentException("minValue cannot be greater than maxValue");
- return NextValue(Seed, false, minValue, maxValue);
- }
- public char NextChar(char minValue, char maxValue)
- {
- if (maxValue <= minValue) throw new ArgumentException("minValue cannot be greater than maxValue");
- Random gnr = new Random(Seed);
- return (char)gnr.Next(minValue, maxValue);
- }
- public bool NextBool(ushort probability)
- {
- if (probability > 100) throw new ArgumentException("minValue cannot be greater than maxValue");
- Random gnr = new Random(Seed);
- if (gnr.NextDouble() * 100 < probability)
- {
- return true;
- }
- return false;
- }
- public DateTime NextDate(DateTime minValue, DateTime maxValue)
- {
- if (maxValue <= minValue) throw new ArgumentException("minValue cannot be greater than maxValue");
- Random gnr = new Random(Seed);
- TimeSpan timeSpan = TimeSpan.FromDays(gnr.Next((maxValue - minValue).Days));
- return minValue + timeSpan;
- }
- }
Add Comment
Please, Sign In to add comment