Return Styles: Pseud0ch, Terminal, Valhalla, NES, Geocities, Blue Moon. Entire thread

percentages

Name: Anonymous 2011-11-09 20:15

Help /prog/ I suck at maths.

I have a range of countries with associated percentages.

How can I base an if condition around that percentage so if I have Switzerland at 44%, there is a 44% chance of evaluating to Switzerland.

Name: Anonymous 2011-11-10 1:28

>>4

This implementation will take linear time in the worst case. If you create an array of key value pairs, where the key is the partial sum of the weights and the value is the object associated with the current weight, and then do binary search on this array using a random number as the key to look for, you will get a log n query time. The array method mentioned above will take constant time, and the number of slots needed isn't necessarily that large. If all of the weights are integers, then you can calculate the greatest common divisor of all the weights, and divide out that factor. Then the total weight will be the sum of these.

Newer Posts
Don't change these.
Name: Email:
Entire Thread Thread List