Suppose there is a array of index and there is a respective wieght of each of them. How can i select the index with probability proportional to its weight?
You will need the total weight stored somewhere first. Next, generate a random number between 0 and the total weight (look up srand() and rand() on this site for generating random numbers). Then, loop through each element of the array adding up the weights until you reach the randomly generated number - stop on this index and choose it!