c13fed0cc1ebfb2d010ca213cfad09d94cec845a kent Wed Oct 31 16:24:45 2012 -0700 Handling an edge condition to avoid a crash. diff --git src/kehayden/alphaChain/alphaChain.c src/kehayden/alphaChain/alphaChain.c index 16cd7e9..5d28c6a 100644 --- src/kehayden/alphaChain/alphaChain.c +++ src/kehayden/alphaChain/alphaChain.c @@ -391,42 +391,45 @@ picked = pickRandomOnUseCounts(list); ++totUseZeroCount; } return picked; } struct wordInfo *pickRandomFromType(struct wordType *type) /* Pick a random word, weighted by outTarget, from all available of given type */ { /* Figure out total on list */ int total = 0; struct wordInfoRef *ref; for (ref = type->list; ref != NULL; ref = ref->next) total += ref->val->outTarget; +if (total > 0) + { /* Loop through list returning selection corresponding to random threshold. */ int threshold = rand() % total; int binStart = 0; for (ref = type->list; ref != NULL; ref = ref->next) { struct wordInfo *info = ref->val; int size = info->outTarget; int binEnd = binStart + size; if (threshold < binEnd) return info; binStart = binEnd; } + } verbose(2, "Fell off end in pickRandomFromType\n"); return type->list->val; // Fall back position (necessary?) just first in list } struct wordTree *predictNextFromAllPredecessors(struct wordTree *wt, struct dlNode *list) /* Predict next word given tree and recently used word list. If tree doesn't * have statistics for what comes next given the words in list, then it returns * NULL. */ { verbose(2, " predictNextFromAllPredecessors(%s, %s)\n", wordTreeString(wt), dlListFragWords(list)); struct dlNode *node; for (node = list; !dlEnd(node); node = node->next) { struct wordInfo *info = node->val;