public void SampleGeometric() { Rand.Restart(96); const double StoppingProbability = 0.7; // The length of sequences sampled from this distribution must follow a geometric distribution StringAutomaton automaton = StringAutomaton.Zero(); automaton.Start = automaton.AddState(); automaton.Start.SetEndWeight(Weight.FromValue(StoppingProbability)); automaton.Start.AddTransition('a', Weight.FromValue(1 - StoppingProbability), automaton.Start); StringDistribution dist = StringDistribution.FromWeightFunction(automaton); var acc = new MeanVarianceAccumulator(); const int SampleCount = 30000; for (int i = 0; i < SampleCount; ++i) { string sample = dist.Sample(); acc.Add(sample.Length); } const double ExpectedMean = (1.0 - StoppingProbability) / StoppingProbability; const double ExpectedVariance = (1.0 - StoppingProbability) / (StoppingProbability * StoppingProbability); Assert.Equal(ExpectedMean, acc.Mean, 1e-2); Assert.Equal(ExpectedVariance, acc.Variance, 1e-2); }