How to get Random values for LogicalTypes

不羁的心 提交于 2019-12-06 03:34:26

问题


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.


回答1:


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.




回答2:


you can create a random number from 0 to enum items count (you can get them by Enum.GetNames().Length), and then cast the number to your LogicalTypeEnum.




回答3:


You could do so with the following code snippet (note: this is just for 1 random value, reuse the Random generator and enumItemsCount for the next item ;) )

var r = new Random();
int enumItemsCount = Enum.GetValues(typeof(LogicalTypeEnum)).Length;
LogicalTypeEnum randomLogicalType = (LogicalTypeEnum)r.Next(enumItemsCount - 1);


来源:https://stackoverflow.com/questions/4884762/how-to-get-random-values-for-logicaltypes

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!