So, this question actually asks two question:
how to find the optimal value of p
given a value of p, how to set this value of p on any participating system
For the first question: I would say that the probability p is chosen "experimentally" through modelling and/or simulation.
Throughput is decreased by both too low and too high values of p. If ...
Usually, the probability p for transmitting when a previously busy channel becomes free again is agreed upon by all nodes.
p is chosen by the installer mostly depending on the total number of nodes in the network and expected transmission rates, but also on the desired aggressiveness. E.g. when the number of nodes is more on the high side but the mean ...