Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get Random values for LogicalTypes

I have a tool that generate entities I need to generate a samlpe value for testing. the problem is that we have alot of logical types (some of the same type but still different) and befor coding i wanted to know if someone have a easier solution...

Here is the Enum :

public enum LogicalTypeEnum
    { 
        Identity,
        DateAndTime,
        Binary,
        Quantity,
        Comment,
        Money,
        Rate,
        TimeStamp,
        Caption,
        Reference,
        Number,
        Weight,
        Space,
        Username,
        Phone,
        Email,
        ZipCode
    }

Thanks!!!

EDIT 1: I want to generate a random value not get a random element from the enum. I'm searching a way to get a random email or zipcode or money value.

like image 254
Polo Avatar asked Feb 03 '11 10:02

Polo


1 Answers

I think you have to divide your answer into two parts:

First get a random enum type out of the list. I think this part is already solved by the other answers provided.

Afterwards you like to create a list of random values for the selected enum. So you need a factory that can create a valid random value for each of these types. The thing that come closest to your needs should be AutoPoco. It is quite easy to create a bunch of sample object filled up with some values you like for example

var factory = AutoPoco.AutoPocoContainer.Configure(x =>
{
    x.Conventions(c =>
    {
        c.UseDefaultConventions();
    });

    x.Include<DataRowWrapper>()
        .Setup(row => row.Timestamp).Use<DateTimeUniqueSource>()
        .Setup(row => row.Name).Use<LastNameSource>()
        .Setup(row => row.Value).Use<ApproximateNumberSource<decimal>>()
        .Setup(row => row.Description).Use<RandomReadableStringSource>(10, 20);
});

var session = factory.CreateSession();
var sampleRows = session.List<DataRowWrapper>(1000).Get();

As you can see you can provide for each property your own Source (.Use<...Source>()). There are already some default sources within the project but i also made some on my own like the following:

public class RandomReadableStringSource : DatasourceBase<string>
{
    private readonly char[] _Vocals = new char[] { 'a', 'e', 'i', 'o', 'u' };
    private readonly char[] _Consonants = new char[] { 'b', 'c', 'd', 'f', 'g', 'h', 'k', 'l', 'm', 'n', 'p', 'r', 's', 't', 'v', 'w' };

    private Random _Random;
    private int _Minimum;
    private int _Maximum;

    public RandomReadableStringSource()
        : this(20)
    { }

    public RandomReadableStringSource(int max)
        : this(5, max)
    { }

    public RandomReadableStringSource(int min, int max)
    {
        if (min <= 0)
        {
            throw new ArgumentOutOfRangeException("minimum must be greater zero.");
        }

        if (min > max)
        {
            throw new ArgumentOutOfRangeException("minimum must be less or equal maximum.");
        }

        _Random = new Random();
        _Minimum = min;
        _Maximum = max;
    }

    public override string Next(IGenerationSession session)
    {
        var length = _Random.Next(_Minimum, _Maximum);
        var sb = new StringBuilder(length);

        for (int i = 0; i < length; i++)
        {
            var array = i % 2 == 0 ? _Consonants : _Vocals;
            sb.Append(array[_Random.Next(array.Length)]);
        }

        return sb.ToString();
    }
}

public class DateTimeUniqueSource : DatasourceBase<DateTime>
{
    private Random _Random;
    private DateTime _LastDateTime;

    public DateTimeUniqueSource()
        : this(new DateTime(1900, 1, 1))
    { }

    public DateTimeUniqueSource(DateTime startdate)
    {
        if (startdate == null)
        {
            throw new ArgumentNullException("startdate");
        }

        _Random = new Random();
        _LastDateTime = startdate;
    }

    public override DateTime Next(IGenerationSession session)
    {
        _LastDateTime = _LastDateTime.AddHours(_Random.NextDouble() * 1000);
        return _LastDateTime;
    }
}

So you could create your own source for each type and afterwards quite easy create a bunch of sample objects.

like image 195
Oliver Avatar answered Oct 16 '22 08:10

Oliver