public FrequencyElementGenerator(IEnumerable <T> elements, IRandom random) { elements.ThrowIfNull("elements"); var counts = new Dictionary <T, float>(); foreach (var element in elements) { if (counts.ContainsKey(element)) { counts[element]++; } else { counts[element] = 1; } } var indexGenerator = new FrequencyIntGenerator(counts.Values, random); elementGenerator = new ListSelectorGenerator <T>(counts.Keys, indexGenerator); }
/// <summary>Constructs a new MarkovChain2IntGenerator /// </summary> /// <param name="frequencies">The conditional frequencies for the elements to generate, /// where frequencies[m][n] is the relative prob of generating n given m was generated /// the last time </param> /// <param name="random">The random generator to use.</param> public MarkovChain2IntGenerator(float[][] frequencies, IRandom random) { int symbolCount = frequencies.Length; var initialFrequencies = new float[symbolCount]; generators = new FrequencyIntGenerator[symbolCount]; for (int i = 0; i < symbolCount; i++) { for (int j = 0; j < symbolCount; j++) { initialFrequencies[j] += frequencies[i][j]; } generators[i] = new FrequencyIntGenerator(frequencies[i], random); } var initialGenerator = new FrequencyIntGenerator(initialFrequencies, random); lastSymbol = initialGenerator.Next(); }