Here's a simple algorithm in python that does what you are expecting.
Let's take for example a single dimension array P equal to [0.1,0.3,0.4,0.2]. The logic can be extended to any number of dimensions.
Now we set each element to the sum of all the elements that precede it:
P => [0, 0.1, 0.4, 0.8, 1]
Using a random generator, we generate numbers that are between 0 and 1. Let's say x = 0.2.
Using a simple binary search, we can determine that x is between the first element and the second element. We just pick the first element for this value of x.
If you look closely, the chance that 0 =< X < 0.1 is 0.1. The chance that 0.1 =< x < 0.4 is 0.3 and so on.
For the 2D array, it is better to convert it to a 1D array, even though, you should be able to implement a 2D array binary search algorithm.