2011-02-03 18 views
5

私はテスト用のsamlpe値を生成するために必要なエンティティを生成するツールを持っています。問題はここ LogicalTypesのランダム値の取得方法

が列挙型である...我々は論理型(まだ同じタイプのいくつかが異なる)の、私は誰かが簡単に解決策を持っているかどうかを知りたいと思ったコーディングbefor多くを持っていることです。

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

ありがとうございます!

EDIT 1:ランダムな値を生成して、列挙型からランダムな要素を取得しません。私はランダムな電子メールまたは郵便番号または金銭価値を得る方法を探しています。

+0

解決策は何よりも簡単ですか?私はあなたが不自然だと思う解決策があると思って、それを掲示してみましょう。 :) – Chris

+0

ここをクリックしてください:http://stackoverflow.com/questions/319814/generate-random-enum-in-c-2-0 –

+0

より簡単にrandomStringを書き込むrandomDouble randomInt randomDateTime ^^ – Polo

答えて

5

私はあなたが分割すると思います2つの部分へのあなたの答え:

まず、リストからランダムな列挙型を取得します。私はこの部分が既に提供されている他の答えによって解決されたと思います。

その後、選択した列挙型のランダムな値のリストを作成することができます。そのため、これらのタイプごとに有効なランダム値を作成できるファクトリが必要です。あなたのニーズに最も近いものはAutoPocoです。あなたは、各プロパティのために、独自のソース(.Use<...Source>())を提供することができます見ることができるようにあなたが例

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(); 

のために好きないくつかの値で埋めサンプルオブジェクトの束を作成することは非常に簡単です。 は、すでにプロジェクト内のいくつかのデフォルトの要因がありますが、私はまた、次のように自分でいくつかを作った:

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; 
    } 
} 

だからあなたがタイプごとに独自のソースを作成することができ、その後、非常に簡単なサンプルオブジェクトの束を作成します。

1

0からenum items countまでの乱数を作成できます(Enum.GetNames().Lengthで取得できます)。LogicalTypeEnumに数値をキャストします。

1

あなたは、次のコードスニペットでそうすることができ(注:これはちょうど1つのランダムな値のためで、次の項目のための乱数発生器とenumItemsCountを再利用;))

var r = new Random(); 
int enumItemsCount = Enum.GetValues(typeof(LogicalTypeEnum)).Length; 
LogicalTypeEnum randomLogicalType = (LogicalTypeEnum)r.Next(enumItemsCount - 1); 
関連する問題