Skip to content

Instantly share code, notes, and snippets.

@ngbrown
Created June 18, 2010 16:14

Revisions

  1. ngbrown created this gist Jun 18, 2010.
    136 changes: 136 additions & 0 deletions RandData.cs
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,136 @@
    namespace UnitTestHelpers
    {
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;

    /// <summary>
    /// Provides random test data
    /// </summary>
    public static class RandData
    {
    private static readonly Random Rand = new Random();
    private static readonly char[] AlphaNumericCharacters = @"0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ".ToCharArray();
    private static readonly char[] PunctuationCharacters = @" .,\/;:-".ToCharArray();

    /// <summary>
    /// Builds random part numbers in the form of xxx-xxxx-xxx. The last section may have up to 5 numbers and letters. (ie. 001AA)
    /// </summary>
    /// <returns><see cref="String"/> of a generated part number.</returns>
    public static string RandomPartNumber()
    {
    var sb = new StringBuilder();

    // generate the digits
    var digits = new int[10];

    for (int i = 0; i < digits.Length; i++)
    {
    digits[i] = Rand.Next(0, 9);
    }

    foreach (var digit in digits)
    {
    sb.Append((char)('0' + digit));
    }

    // is it a snapshot? (25% odds)
    if (Rand.NextDouble() < 0.25)
    {
    // generate a snapshot value
    int snapshotNum = 1;

    // the odds of a successive snapshot is increasingly rare (75% of the time)
    while (Rand.NextDouble() < 0.75)
    {
    snapshotNum++;
    }

    if (snapshotNum > ('Z' - 'A'))
    {
    sb.Append((char)('A' + snapshotNum - ('Z' - 'A')));
    snapshotNum = snapshotNum - ('Z' - 'A');
    }

    sb.Append((char)('A' + snapshotNum));
    }

    // add the dashes
    sb.Insert(3, '-');
    sb.Insert(8, '-');

    return sb.ToString();
    }

    /// <summary>
    /// Builds random text of the specified length.
    /// </summary>
    /// <param name="minLength">Length of the min.</param>
    /// <param name="maxLength">Length of the max.</param>
    /// <returns><see cref="String"/> of a generated phrase</returns>
    public static string Text(int minLength, int maxLength)
    {
    var sb = new StringBuilder();

    var stringLength = Rand.Next(minLength, maxLength);

    for (int i = 0; i < stringLength; i++)
    {
    if (Rand.NextDouble() < 0.10)
    {
    // add punctuation
    sb.Append(PunctuationCharacters[Rand.Next(0, PunctuationCharacters.Length - 1)]);
    }
    else
    {
    // add characters
    sb.Append(AlphaNumericCharacters[Rand.Next(0, AlphaNumericCharacters.Length - 1)]);
    }
    }

    return sb.ToString();
    }

    /// <summary>
    /// Builds random alpha numeric text of the specified length.
    /// </summary>
    /// <param name="minLength">Length of the min.</param>
    /// <param name="maxLength">Length of the max.</param>
    /// <returns><see cref="String"/> of a generated phrase</returns>
    public static string Alphanumeric(int minLength, int maxLength)
    {
    var sb = new StringBuilder();

    var stringLength = Rand.Next(minLength, maxLength);

    for (int i = 0; i < stringLength; i++)
    {
    // add characters
    sb.Append(AlphaNumericCharacters[Rand.Next(0, AlphaNumericCharacters.Length - 1)]);
    }

    return sb.ToString();
    }

    /// <summary>
    /// Returns a random number between the min and max value.
    /// </summary>
    /// <param name="minValue">The min value.</param>
    /// <param name="maxValue">The max value.</param>
    /// <returns>A double-precision floating point number greater than or equal to minValue, and less than maxValue.</returns>
    public static double Double(double minValue, double maxValue)
    {
    return (Rand.NextDouble() * (maxValue - minValue)) + minValue;
    }

    /// <summary>
    /// Returns a random number between 0.0 and 1.0.
    /// </summary>
    /// <returns>A double-precision floating point number greater than or equal to 0.0, and less than 1.0.</returns>
    public static double Double()
    {
    return Rand.NextDouble();
    }
    }
    }