// doTest checks that Sample picks items evenly. "n" samples are // drawn from a list of numbers of length "space". This is repeated // "iters" times. The number of times a particular number is drawn // should be within "tolerance" of the expected number. func doTest(t *testing.T, n, space, iters int, tolerance float64) { count := make([]int, space) for i := 0; i < iters; i++ { s := stream.Sequence( stream.Numbers(0, space-1), stream.SampleWithSeed(n, int64(i)), ) stream.ForEach(s, func(s string) { num := -1 // Will cause panic below if Scan fails fmt.Sscan(s, &num) count[num]++ }) } // Check that all counts are approximately equal. expected := (float64(iters) * float64(n)) / float64(space) minExpected := expected * (1.0 - tolerance) maxExpected := expected * (1.0 + tolerance) for i, n := range count { if float64(n) < minExpected || float64(n) > maxExpected { t.Errorf("%d has %d samples; expected range [%f,%f]\n", i, n, minExpected, maxExpected) } } }
func ExampleSampleWithSeed() { stream.Run( stream.Numbers(1, 100), stream.SampleWithSeed(2, 100), stream.Sort().Num(1), stream.WriteLines(os.Stdout), ) // Output: // 11 // 46 }