i want generate not repeat random #385
Answered
by
bchavez
HelloValue
asked this question in
Q&A
-
this is my code:
i expect items return different itemcode. |
Beta Was this translation helpful? Give feedback.
Answered by
bchavez
Jul 23, 2021
Replies: 1 comment 1 reply
-
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];
ItemCodes.RemoveAt(i);
return val;
}
} |
Beta Was this translation helpful? Give feedback.
1 reply
Answer selected by
bchavez
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
If you don't want to repeat random values, then you need to maintain state: