func randomTransition(ch *markovchain.Chain, state markovchain.State) markovchain.State { entry := ch.Lookup(state) if entry == nil || len(entry.Targets) == 0 { return nil } prob := rand.Float64() var curProb float64 for i, x := range entry.Probabilities { curProb += x if curProb > prob { return entry.Targets[i] } } return entry.Targets[len(entry.Targets)-1] }
func randomStart(ch *markovchain.Chain) markovchain.State { var allStates []markovchain.State ch.Iterate(func(s *markovchain.StateTransitions) bool { allStates = append(allStates, s.State) return true }) state := allStates[rand.Intn(len(allStates))] // Run through the markov chain to land at a more // likely state. for i := 0; i < 10; i++ { newState := randomTransition(ch, state) if newState == nil { break } state = newState } return state }