From: weiyu Date: Fri, 13 Dec 2019 00:02:29 +0000 (-0800) Subject: Weight-based random walk X-Git-Url: http://demsky.eecs.uci.edu/git/?a=commitdiff_plain;h=5fdda11a1d60807f3bf248dc4cc277b66ddf7592;p=c11tester.git Weight-based random walk --- diff --git a/newfuzzer.cc b/newfuzzer.cc index b98d9a4b..29961990 100644 --- a/newfuzzer.cc +++ b/newfuzzer.cc @@ -176,7 +176,32 @@ Predicate * NewFuzzer::selectBranch(thread_id_t tid, Predicate * curr_pred, Func */ int NewFuzzer::choose_index(SnapVector * branches) { - return random() % branches->size(); + if (branches->size() == 1) + return 0; + + double total_weight = 0; + SnapVector weights; + for (uint i = 0; i < branches->size(); i++) { + Predicate * branch = (*branches)[i]; + double weight = branch->get_weight(); + total_weight += weight; + weights.push_back(weight); + } + + double prob = (double) random() / RAND_MAX; + double prob_sum = 0; + int index = 0; + + for (uint i = 0; i < weights.size(); i++) { + index = i; + prob_sum += (double) (weights[i] / total_weight); + if (prob_sum > prob) { + break; + } + } + + return index; +// return random() % branches->size(); } Predicate * NewFuzzer::get_selected_child_branch(thread_id_t tid)