- // randomly select a branch
- int random_index = random() % branches.size();
- Predicate * random_branch = branches[ random_index ];
- thrd_selected_child_branch[thread_id] = random_branch;
+ int index = choose_branch_index(&available_branches_tmp_storage);
+ Predicate * selected_branch = available_branches_tmp_storage[ index ];
+
+ /* Remove the chosen branch from vec in case that this
+ * branch fails and need to choose another one */
+ available_branches_tmp_storage[index] = available_branches_tmp_storage.back();
+ available_branches_tmp_storage.pop_back();
+
+ return selected_branch;
+}
+
+/**
+ * @brief Select a branch from the given predicate branch
+ */
+int NewFuzzer::choose_branch_index(SnapVector<Predicate *> * branches)
+{
+ if (branches->size() == 1)
+ return 0;
+
+ double total_weight = 0;
+ SnapVector<double> 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;
+ }
+ }