Example #1
0
        /// <summary>
        /// Random date between dates, except for a specific date.
        /// </summary>
        /// <param name="from">Minimum date. Must be less than maximum and excepted date.</param>
        /// <param name="to">Maximum date. Must be greater than minimum and excepted date.</param>
        /// <param name="excepted">Excepted date.</param>
        /// <returns></returns>
        /// <exception cref="ArgumentException">If <paramref name="from"/>, <paramref name="to"/> and <paramref name="excepted"/> are equal.</exception>
        /// <exception cref="ArgumentOutOfRangeException">If <paramref name="to"/> is less than <paramref name="from"/> or if <paramref name="excepted"/> outside of date range.</exception>
        /// <remarks>Minimum date and maximum date are included in the range from which a random date is picked.</remarks>
        public static DateTime BetweenExcept(DateTime from, DateTime to, DateTime excepted)
        {
            DateTime fromDate     = from.Date;
            DateTime toDate       = to.Date;
            DateTime exceptedDate = excepted.Date;

            if (fromDate == toDate && toDate == exceptedDate)
            {
                throw new ArgumentException("From date, to date and excepted date must not be the same.");
            }
            if (toDate < fromDate)
            {
                throw new ArgumentOutOfRangeException(nameof(to), $"Must be equal to or greater than {nameof(from)}.");
            }
            if (exceptedDate < from || excepted > to)
            {
                throw new ArgumentOutOfRangeException(nameof(excepted), $"Must be between {nameof(from)} and {nameof(to)} date.");
            }
            var      timespanDays = (int)(toDate - fromDate).TotalDays;
            DateTime result;

            do
            {
                result = from.Date.AddDays(RandomNumber.Next(timespanDays)).Date;
            } while (result == excepted);
            return(result);
        }
Example #2
0
        /// <summary>
        /// Generate a credit card number, optionally from a selection of types. Example: "3018-348979-1853". For available types
        /// <see cref="CreditCardType"/>.
        /// </summary>
        /// <param name="types">Zero or more credit card types to choose from. Default is all.</param>
        /// <returns></returns>
        public static string CreditCard(params CreditCardType[] types)
        {
            if (types.Length == 0)
            {
                types = new[] { CreditCardType.Visa, CreditCardType.Mastercard, CreditCardType.Discover,
                                CreditCardType.AmericanExpress, CreditCardType.DinersClub, CreditCardType.Jcb, CreditCardType.Switch,
                                CreditCardType.Solo, CreditCardType.Dankort, CreditCardType.Maestro, CreditCardType.Forbrugsforeningen,
                                CreditCardType.Laser };
            }

            var    type           = types.Sample();
            string typeAsString   = type.ToString();
            var    parts          = Regex.Matches(typeAsString, @"[A-Z]{1}[a-z]{1,}").Cast <Match>().Select(m => m.Value).ToArray();
            var    partsList      = new List <string>(parts).ConvertAll(d => d.ToLower());
            var    yamlIdentifier = string.Join("_", partsList);
            var    template       = Fetch($"finance.credit_card.{yamlIdentifier}").Trim('/').Numerify();

            template = Regex.Replace(template, @"\[(\d)-(\d)\]", range => {
                return(RandomNumber.Next(int.Parse(range.Groups[1].Value), int.Parse(range.Groups[2].Value)).ToString());
            }, RegexOptions.Compiled);

            // Luhn check digit if required:
            var checkDigit = Regex.Replace(template, @"[^0-9]", string.Empty, RegexOptions.Compiled).ToDigitList().LuhnCheckDigit();

            template = template.Replace("L", checkDigit.ToString());
            return(template);
        }
Example #3
0
        /// <summary>
        /// Returns an IP v6 address without CIDR suffix. Example: "ac5f:d696:3807:1d72:2eb5:4e81:7d2b:e1df"
        /// </summary>
        /// <remarks>CIDR = Classless Inter-Domain Routing.</remarks>
        /// <returns>An IPv6 address without CIDR suffix</returns>
        public static string IPv6Address()
        {
            var list = new List <string>();

            8.TimesDo(_ => list.Add($"{RandomNumber.Next(65536):x4}"));
            return(string.Join(":", list));
        }
Example #4
0
        /// <summary>
        /// Generates a private IP v4 address. The result will not include the CIDR suffix. Example: "10.0.0.1"
        /// </summary>
        /// <remarks>CIDR = Classless Inter-Domain Routing.</remarks>
        /// <returns>A private IPv4 Address as a string without CIDR suffix.</returns>
        public static string PrivateIPv4Address()
        {
            switch (RandomNumber.Next(1, 8))
            {
            case 1: // 10.0.0.0    - 10.255.255.255
                return($"10.{RandomNumber.Next(255)}.{RandomNumber.Next(255)}.{RandomNumber.Next(1,255)}");

            case 2: // 100.64.0.0  - 100.127.255.255
                return($"100.{RandomNumber.Next(64,127)}.{RandomNumber.Next(255)}.{RandomNumber.Next(1,255)}");

            case 3: // 127.0.0.0   - 127.255.255.255
                return($"127.{RandomNumber.Next(255)}.{RandomNumber.Next(255)}.{RandomNumber.Next(1,255)}");

            case 4: // 169.254.0.0 - 169.254.255.255
                return($"169.254.{RandomNumber.Next(255)}.{RandomNumber.Next(1,255)}");

            case 5: // 172.16.0.0  - 172.31.255.255
                return($"172.{RandomNumber.Next(16,31)}.{RandomNumber.Next(255)}.{RandomNumber.Next(1,255)}");

            case 6: // 192.0.0.0   - 192.0.0.255
                return($"192.0.0.{RandomNumber.Next(1,255)}");

            case 7: // 192.168.0.0   - 192.168.255.255
                return($"192.168.{RandomNumber.Next(255)}.{RandomNumber.Next(1,255)}");

            default: // 192.168.0.0   - 192.168.255.255
                return($"192.{RandomNumber.Next(18,19)}.{RandomNumber.Next(255)}.{RandomNumber.Next(1,255)}");
            }
        }
Example #5
0
        /// <summary>
        /// Generates a password. Example: "*%NkOnJsH4"
        /// </summary>
        /// <param name="minLength">Minimum length of the password. Default value is 8.</param>
        /// <param name="maxLength">Maximum length of the password. Default value is 15.</param>
        /// <param name="mixCase">Flag whether to use upper case characters or not. Default value is true (i.e. use upper case).</param>
        /// <param name="specialChars">Flag whether to use special characters (!@#$%^&amp;&#42;). Default value is true (i.e. use special characters).</param>
        /// <exception cref="ArgumentOutOfRangeException">If minLength or maxLength are outside of the valid ranges.</exception>
        /// <returns>A password</returns>
        public static string Password(int minLength = 8, int maxLength = 15, bool mixCase = true, bool specialChars = true)
        {
            if (minLength < 1)
            {
                throw new ArgumentOutOfRangeException(nameof(minLength), "Must be greater than zero.");
            }
            if (maxLength < 1)
            {
                throw new ArgumentOutOfRangeException(nameof(maxLength), "Must be greater than zero.");
            }
            if (maxLength < minLength)
            {
                throw new ArgumentOutOfRangeException(nameof(maxLength), $"Must be equal to or greater than {nameof(minLength)}.");
            }
            var temp       = Lorem.Characters(minLength);
            var diffLength = maxLength - minLength;

            if (diffLength > 0)
            {
                var diffRand = RandomNumber.Next(diffLength + 1);
                temp += Lorem.Characters(diffRand);
            }
            if (mixCase)
            {
                temp = EnsureMixCase(temp);
            }
            if (specialChars)
            {
                const string chars = "!@#$%^&*";
                var          sb    = new StringBuilder(temp);
                RandomNumber.Next(1, minLength).TimesDo(i => sb[i] = chars.Sample()[0]);
                temp = sb.ToString();
            }
            return(temp);
        }
Example #6
0
        private static List <int> FrenchSirenNumberDigits()
        {
            var digits = new List <int>();

            digits.AddRange(8.Times(x => RandomNumber.Next(10)));
            digits.AppendLuhnCheckDigit();
            return(digits);
        }
Example #7
0
        internal static string EnsureMixCase(string input)
        {
            var indexToReplace = RandomNumber.Next(0, input.Length);
            var sb             = new StringBuilder(input);

            sb[indexToReplace] = "ABCDEFGHIJKLMNOPQRSTUVWXYZ".Sample()[0];
            return(sb.ToString());
        }
Example #8
0
 /// <summary>
 /// Returns a URL to a random fake company logo in PNG format.
 /// </summary>
 /// <returns></returns>
 public static string Logo()
 {
     // Selection of fake but convincing logos for real-looking test data:
     // https://github.com/pigment/fake-logos
     // Available under a Creative Commons (CC) "Attribution-ShareAlike 4.0 International (CC BY-SA 4.0)
     // license. License https://creativecommons.org/licenses/by-sa/4.0/
     return($"https://pigment.github.io/fake-logos/logos/medium/color/#{RandomNumber.Next(13)}.png");
 }
Example #9
0
        /// <summary>
        /// Get a random French SIRET number. Example: "81948962600013"
        /// </summary>
        /// <returns></returns>
        public static string FrenchSiretNumber()
        {
            var location = RandomNumber.Next(100).ToString().PadLeft(4, '0');
            var digits   = new List <int>(FrenchSirenNumberDigits());

            digits.AddRange(location.Select(x => int.Parse(x.ToString())));
            digits.AppendLuhnCheckDigit();
            return(ConvertToString(digits));
        }
Example #10
0
        /// <summary>
        /// Gets a random Spanish organization number. Example: "P2344979"
        /// </summary>
        /// <returns></returns>
        public static string SpanishOrganizationNumber()
        {
            // Valid leading characters: A, B, C, D, E, F, G, H, J, N, P, Q, R, S, U, V, W
            // 7 digit numbers
            var letters = "A, B, C, D, E, F, G, H, J, N, P, Q, R, S, U, V, W".Replace(",", string.Empty).Split(' ');
            var digits  = new List <int>();

            digits.AddRange(7.Times(x => RandomNumber.Next(10)));
            return($"{letters.Sample()}{ConvertToString(digits)}");
        }
Example #11
0
        /// <summary>
        /// Generates a MAC address, optionally with a given prefix. Example: "e6:0d:00:11:ed:4f"
        /// </summary>
        /// <param name="prefix">Desired prefix, e.g. "55:af:33". Default value is "".</param>
        /// <returns>A MAC address</returns>
        /// <exception cref="ArgumentNullException">Value of 'prefix' was null (nothing in VB.NET).</exception>
        public static string MacAddress(string prefix = "")
        {
            if (prefix == null)
            {
                throw new ArgumentNullException(nameof(prefix), "Must not be null.");
            }
            string[] parts         = Regex.Split(prefix, ":");
            var      prefixDigits  = parts.Where(x => !string.IsNullOrWhiteSpace(x)).Select(x => int.Parse(x, NumberStyles.HexNumber));
            var      addressDigits = (6 - prefixDigits.Count()).Times(_ => RandomNumber.Next(256));

            return(string.Join(":", prefixDigits.Concat(addressDigits).Select(x => $"{x:x2}")));
        }
Example #12
0
        /// <summary>
        /// Generates a GSM modem, device or mobile phone 15 digit IMEI number.
        /// </summary>
        /// <returns></returns>
        public static string Imei()
        {
            // Fill in the first two values of the string based with the specified prefix.
            // Reporting Body Identifier list: http://en.wikipedia.org/wiki/Reporting_Body_Identifier
            int reportingBodyIdentifier = _RBI.Sample();
            var arr = $"{reportingBodyIdentifier}".PadLeft(2, '0').ToDigitList();

            12.TimesDo(_ => arr.Add(RandomNumber.Next(0, 10)));
            var digits = arr.AppendLuhnCheckDigit();

            return(string.Join("", digits));
        }
Example #13
0
        /// <summary>
        /// Random date between dates.
        /// </summary>
        /// <param name="from">Minimum date.</param>
        /// <param name="to">Maximum date. Must be equal or greater than 'minDate'.</param>
        /// <returns></returns>
        /// <exception cref="ArgumentOutOfRangeException">When <paramref name="to"/> is less than <paramref name="from"/>.</exception>
        public static DateTime Between(DateTime from, DateTime to)
        {
            var fromDate = from.Date;
            var toDate   = to.Date;

            if (toDate < fromDate)
            {
                throw new ArgumentOutOfRangeException(nameof(to), $"Must be equal to or greater than {nameof(from)}.");
            }
            var timespan = toDate - fromDate;

            return(fromDate.AddDays(RandomNumber.Next((int)timespan.TotalDays)).Date);
        }
Example #14
0
        /// <summary>
        /// Generate a valid US Social Security Number, checking if any of the
        /// segements is all zeros.
        /// </summary>
        /// <returns></returns>
        public static string SsnValid()
        {
            var random1 = RandomNumber.Next(1, 899);

            while (random1 == 666)
            {
                random1 = RandomNumber.Next(1, 899);
            }
            var random2 = RandomNumber.Next(1, 99);
            var random3 = RandomNumber.Next(1, 9999);

            return($"{random1:000}-{random2:00}-{random3:00}");
        }
Example #15
0
        /// <summary>
        /// Generates a random paragraph, optionally considering words from a supplementary list of
        /// Lorem-like words. Example: Paragraph() returns a sentence similar to "Neque dicta enim quasi.
        /// Qui corrupti est quisquam. Facere animi quod aut. Qui nulla consequuntur consectetur sapiente."
        /// </summary>
        /// <param name="sentenceCount">Number of sentences. Zero is a valid value. Default value is 3.</param>
        /// <param name="supplemental">Flag to indicate whether to consider words from a supplementary list of Lorem-like words. Default is false.</param>
        /// <param name="randomSentencesToAdd">Number of random sentences to add. Default value 0.</param>
        /// <returns></returns>
        /// <exception cref="ArgumentOutOfRangeException">If 'sentenceCount' or 'randomSentencesToAdd' is less than zero.</exception>
        public static string Paragraph(int sentenceCount = 3, bool supplemental = false, int randomSentencesToAdd = 0)
        {
            if (sentenceCount < 0)
            {
                throw new ArgumentOutOfRangeException(nameof(sentenceCount), "Must be equal to or greater than zero.");
            }
            if (randomSentencesToAdd < 0)
            {
                throw new ArgumentOutOfRangeException(nameof(randomSentencesToAdd), "Must be equal to or greater than zero.");
            }

            return(string.Join(PunctuationSpace(), Sentences(sentenceCount + RandomNumber.Next(randomSentencesToAdd), supplemental).ToArray()));
        }
Example #16
0
        /// <summary>
        /// Get a random Czech organization number. Example: "77778171"
        /// </summary>
        /// <returns></returns>
        public static string CzechOrganizationNumber()
        {
            var sum     = 0;
            var digits  = new List <int>();
            var weights = new int[] { 8, 7, 6, 5, 4, 3, 2 };

            foreach (var weight in weights)
            {
                digits.Add(RandomNumber.Next(10));
                sum += (weight * digits.Last());
            }
            digits.Add((11 - (sum % 11)) % 10);
            return(ConvertToString(digits));
        }
Example #17
0
        /// <summary>
        /// Gets a random Australian business number. Example: "81137773602"
        /// </summary>
        /// <returns></returns>
        public static string AustralianBusinessNumber()
        {
            var @base = new List <int>();

            @base.AddRange(9.Times(x => RandomNumber.Next(10)));
            var toCheck = new List <int>();

            toCheck.AddRange(2.Times(x => 0));
            toCheck.AddRange(@base);
            var digits = new List <int>();

            digits.Add(99 - (AbnChecksum(toCheck) % 89));
            digits.AddRange(@base);
            return(ConvertToString(digits));
        }
Example #18
0
        /// <summary>
        /// Get a random Polish register of national economy number.
        /// </summary>
        /// <param name="length">Length of number. Valid values are 9 and 14.</param>
        /// <returns></returns>
        /// <exception cref="ArgumentOutOfRangeException">Thrown when 'length' is a value other than 9 or 14.</exception>
        public static string PolishRegisterOfNationalEconomy(int length = 9)
        {
            // More info https://pl.wikipedia.org/wiki/REGON
            if (length != 9 && length != 14)
            {
                throw new ArgumentOutOfRangeException(nameof(length), "Must be either 9 or 14.");
            }
            List <int> digits;

            do
            {
                digits = new List <int>();
                digits.AddRange(length.Times(x => RandomNumber.Next(10)));
            } while (CollectRegonSum(digits) != digits.Last());
            return(ConvertToString(digits));
        }
Example #19
0
        /// <summary>
        /// Get a random Polish taxpayer identification number. Example: "1060000062"
        /// </summary>
        /// <returns></returns>
        public static string PolishTaxpayerIdentificationNumber()
        {
            // More info https://pl.wikipedia.org/wiki/NIP
            List <int> digits;
            var        weights = new List <int> {
                6, 5, 7, 2, 3, 4, 5, 6, 7
            };

            do
            {
                digits = new List <int>();
                digits.AddRange(3.Times(x => RandomNumber.Next(1, 9)));
                digits.AddRange(7.Times(x => RandomNumber.Next(10)));
            } while (WeightSum(digits, weights) % 11 != digits[9]);
            return(ConvertToString(digits));
        }
Example #20
0
        /// <summary>
        /// Get a random Norwegian organization number. Example: "839071558"
        /// </summary>
        /// <returns></returns>
        public static string NorwegianOrganizationNumber()
        {
            // Valid leading digits: 8, 9
            int?       mod11Check = null;
            List <int> digits     = new List <int>();

            while (mod11Check == null)
            {
                digits = new List <int> {
                    RandomNumber.Next(8, 10)
                };
                digits.AddRange(7.Times(x => RandomNumber.Next(10)));
                mod11Check = digits.Mod11();
            }
            digits.Add(mod11Check.Value);
            return(ConvertToString(digits));
        }
Example #21
0
        /// <summary>
        /// Generates a random Swedish organization number. See more here https://sv.wikipedia.org/wiki/Organisationsnummer
        /// </summary>
        /// <returns></returns>
        public static string SwedishOrganizationNumber()
        {
            // Valid leading digit: 1, 2, 3, 5, 6, 7, 8, 9
            // Valid third digit: >=2
            // Last digit is a control digit
            var validLeadingDigits = new List <int> {
                1, 2, 3, 5, 6, 7, 8, 9
            };
            var digits = new List <int> {
                validLeadingDigits.Sample(),
                RandomNumber.Next(10),
                RandomNumber.Next(2, 10)
            };

            digits.AddRange(6.Times(x => RandomNumber.Next(10)));
            digits.Add(digits.LuhnCheckDigit());
            return(ConvertToString(digits));
        }
Example #22
0
        /// <summary>
        /// Generates a capitalised sentence of random words. To specify an exact word count for a
        /// sentence, set wordCount to the number you want and randomWordsToAdd equal to 0. By
        /// default (i.e. a call without any parameter values), sentences will have 4 words.
        /// </summary>
        /// <param name="wordCount">Minimum number of words in the sentence. Default is 4.</param>
        /// <param name="supplemental">Flag to indicate whether to consider words from a supplementary list of Lorem-like words. Default is false.</param>
        /// <param name="randomWordsToAdd">The 'randomWordsToAdd' argument increases the sentence's word
        /// count by a random value within the range (0..randomWordsToAdd). Default value is 0.</param>
        /// <returns></returns>
        /// <exception cref="ArgumentOutOfRangeException">If either 'wordCount' or 'randomWordsToAdd' is less than zero.</exception>
        public static string Sentence(int wordCount = 4, bool supplemental = false, int randomWordsToAdd = 0)
        {
            if (wordCount < 0)
            {
                throw new ArgumentOutOfRangeException(nameof(wordCount), "Must be equal to or greater than zero.");
            }
            if (randomWordsToAdd < 0)
            {
                throw new ArgumentOutOfRangeException(nameof(randomWordsToAdd), "Must be equal to or greater than zero.");
            }
            //string sentence = string.Join(Fetch("lorem.punctuation.space"), Words(wordCount + RandomNumber.Next(randomWordsToAdd), supplemental).ToArray()).Capitalise();
            string sentence = string.Join(PunctuationSpace(), Words(wordCount + RandomNumber.Next(randomWordsToAdd), supplemental).ToArray()).Capitalise();

            if (sentence.Length > 0)
            {
                sentence += PunctuationPeriod();
            }
            return(sentence);
        }
Example #23
0
        /// <summary>
        /// Generates Social Insurance Number issued in Canada.
        /// </summary>
        /// <returns></returns>
        public static string Sin()
        {
            // https://en.wikipedia.org/wiki/Social_Insurance_Number
            // 1   - province or temporary resident
            // 2-8 - random numbers
            // 9   - checksum

            // 1st digit. 8,0 are not used.
            var digits = new List <int> {
                new Range2 <int>(1, 9).AsArray().Sample()
            };

            // generate 2nd to 8th
            7.TimesDo(_ => digits.Add(RandomNumber.Next(0, 10)));

            // generate 9th digit
            digits.Add(GenerateSinCheckDigit(digits));

            return(string.Join("", digits));
        }
Example #24
0
        /// <summary>
        /// Returns a paragraph with a specified amount of characters, optionally considering words
        /// from a supplementary list of Lorem-like words. If needed the last word may be truncated.
        /// Example: ParagraphByChars() returns something similar to "Truffaut stumptown trust fund 8-bit
        /// messenger bag portland. Meh kombucha selvage swag biodiesel. Lomo kinfolk jean shorts
        /// asymmetrical diy. Wayfarers portland twee stumptown. Wes anderson biodiesel retro 90's pabst.
        /// Diy echo 90's mixtape semiotics. Cornho."
        /// </summary>
        /// <param name="chars">Number of characters in the paragraph. Zero is a valid value. Default value is 256.</param>
        /// <param name="supplemental">Flag to indicate whether to consider words from a supplementary list of Lorem-like words. Default is false.</param>
        /// <returns></returns>
        /// <remarks>If the sentence would add in " ." then the space is replaced by a random letter. In other words that
        /// random letter is appended to the last word.</remarks>
        public static string ParagraphByChars(int chars = 256, bool supplemental = false)
        {
            if (chars < 0)
            {
                throw new ArgumentOutOfRangeException(nameof(chars), "Must be equal to or greater than zero.");
            }
            var paragraph = Paragraph(3, supplemental);

            while (paragraph.Length < chars)
            {
                paragraph += PunctuationSpace() + Paragraph(3, supplemental);
            }
            paragraph = paragraph.Substring(0, chars);
            if (paragraph.EndsWith(PunctuationSpace()))
            {
                // pad with random letter if paragraph would end in " ." otherwise
                paragraph = paragraph.Trim() + _characters[RandomNumber.Next(10, _characters.Length)];
            }
            if (!string.IsNullOrWhiteSpace(paragraph))
            {
                paragraph += PunctuationPeriod();
            }
            return(paragraph);
        }
Example #25
0
 /// <summary>
 /// Generates a credit card expiry date.
 /// </summary>
 /// <returns></returns>
 public static DateTime CreditCardExpiryDate()
 {
     return(DateTime.Today.Date.AddDays(365 * RandomNumber.Next(1, 4)));
 }
Example #26
0
        /// <summary>
        /// Returns a random hex color, e.g. "45AF55"
        /// </summary>
        /// <returns></returns>
        public static string HexColor()
        {
            var random = RandomNumber.Next(0, (int)Math.Pow(16, 6));

            return($"{random:X}".PadLeft(6, '0'));
        }
Example #27
0
 /// <summary>
 /// Returns an array with three elements, representing an RGB value.
 /// </summary>
 /// <returns></returns>
 public static byte[] RgbColor()
 {
     return(new byte[] { Convert.ToByte(RandomNumber.Next(0, 256)), Convert.ToByte(RandomNumber.Next(0, 256)), Convert.ToByte(RandomNumber.Next(0, 256)) });
 }
Example #28
0
 /// <summary>
 /// Returns an IP v4 address including CIDR suffix. Example: "24.29.18.175/21"
 /// </summary>
 /// <remarks>CIDR = Classless Inter-Domain Routing.</remarks>
 /// <returns>An IPv4 address including CIDR suffix</returns>
 public static string IPv4CIDR()
 {
     // For details about CIDR see https://en.wikipedia.org/wiki/Classless_Inter-Domain_Routing
     return($"{IPv4Address()}/{RandomNumber.Next(1, 31)}");
 }
Example #29
0
 private static string NonZeroDigit()
 {
     return(RandomNumber.Next(1, 10).ToString());
 }
Example #30
0
 /// <summary>
 /// Generates a 10 digit NPI (National Provider Identifier issued to health care
 /// providers in the United States.
 /// </summary>
 /// <returns></returns>
 public static string Npi()
 {
     return(RandomNumber.Next(Math.Pow(10, 10)).ToString().PadLeft(10, '0'));
 }