Applying distributions when generating collections #541
Closed
davidkeaveny
started this conversation in
Ideas
Replies: 1 comment 1 reply
-
|
Hi @davidkeaveny , there is a Here's an example: void Main()
{
var f = new Faker();
var items = Enum.GetValues<PassportCategory>();
items.Dump();
float[] weights = [0.30f, 0.30f, 0.20f, 0.10f, 0.10f];
var selections = Enumerable.Range(1,100)
.Select( _ => f.Random.WeightedRandom<PassportCategory>(items, weights))
.ToArray();
selections.Count().Dump();
var british = selections.Count(i => i is PassportCategory.British);
var euro = selections.Count(i => i is PassportCategory.European);
var asian = selections.Count(i => i is PassportCategory.Asian);
var american = selections.Count(i => i is PassportCategory.American);
var other = selections.Count(i => i is PassportCategory.Other);
british.Dump();
euro.Dump();
asian.Dump();
american.Dump();
other.Dump();
}
public enum PassportCategory
{
British = 0,
European = 1,
Asian = 2,
American = 3,
Other = 4
}Of course, your numbers might not be exactly the same, but similar since I'm not setting a seed value. Hope that helps. |
Beta Was this translation helpful? Give feedback.
1 reply
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
-
Has anyone ever had a need to apply non-even distributions when selecting random values?
For instance, if I were creating a fake dataset of passengers arriving at the international terminal of London Heathrow airport, I might want it to be semi-realistic in that passengers held passports in the following distribution:
This obviously would allow us to model e.g. passenger flow through immigration, with its different channels depending on the passport you hold.
If I were to define an enum, then I could use the
PickRandom<T>method, but that would give an even distribution.I could define a map that gives the distribution I want, and pick from that, but that doesn't scale well:
Beta Was this translation helpful? Give feedback.
All reactions