Search
 
SCRIPT & CODE EXAMPLE
 
CODE EXAMPLE FOR C

choose random number with weight

int sum_of_weight = 0;
for(int i=0; i<num_choices; i++) {
   sum_of_weight += choice_weight[i];
}
int rnd = random(sum_of_weight);
for(int i=0; i<num_choices; i++) {
  if(rnd < choice_weight[i])
    return i;
  rnd -= choice_weight[i];
}
assert(!"should never get here");
Source by stackoverflow.com #
 
PREVIOUS NEXT
Tagged: #choose #random #number #weight
ADD COMMENT
Topic
Name
7+4 =