Skip to content

i want generate not repeat random #385

Answered by bchavez
HelloValue asked this question in Q&A
Discussion options

You must be logged in to vote

If you don't want to repeat random values, then you need to maintain state:

void Main()
{
   var testitemsFaker = new Faker<TestItem>()
             .RuleFor(o => o.ItemCode, f => f.Random.ItemCode());

   while( true )
   {
      testitemsFaker.Generate().Dump();
   }
}

public static List<string> ItemCodes = new List<string>(){ "ALT","PA","CRE","MG" };

public class TestItem
{
   public string ItemCode;
}

public static class ExtensionsForBogus
{
   public static string ItemCode(this Randomizer r)
   {
      if (ItemCodes.Count == 0) throw new Exception("No more item codes without repeating.");
      var i = r.Number(min: 0, max: ItemCodes.Count - 1);
      var val = ItemCodes[i];

Replies: 1 comment 1 reply

Comment options

You must be logged in to vote
1 reply
@HelloValue
Comment options

Answer selected by bchavez
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
2 participants