I want to generate a number based on a distributed probability. For example, just say there are the following occurences of each numbers:
Number| Count
Do this only once:
Do this every time:
The running time will be proportional to log of the size of the given pdf array. Which is good. However, if your array size will always be so small (4 in your example) then performing a linear search is easier and also will perform better.