You're on the right track. Your code should continue as follows:
...
let buy random-float P1 + P2 + P3
ifelse buy < P1 [
do-stuff-with-P1
] [
ifelse buy < P1 + P2 [
do-stuff-with-P2
] [
do-stuff-with-P3
]
]
You can generalize this with a function that takes a list, and it's a bit nicer in my opinion:
to-report weighted-random [ weights ]
let pick random-float sum weights
let total 0
let i 0
foreach weights [
set total total + ?
if pick < total [ report i ]
set i i + 1
]
end
You'd use this as follows:
let buy weighted-random (list P1 P2 P3)
if buy = 0 [ do-stuff-with-P1 ]
if buy = 1 [ do-stuff-with-P2 ]
if buy = 2 [ do-stuff-with-P3 ]