We develop a flexible and accurate framework for device-to-device (D2D) communication in the context of a conventional cellular network, which allows for time-frequency resources to be either shared or orthogonally partitioned between the two networks. Using stochastic geometry, we provide accurate expressions for SINR distributions and average rates, under an assumption of interference randomization via time and/or frequency hopping, for both dedicated and shared spectrum approaches. We obtain analytical results in closed or semi-closed form in high SNR regime, that allow us to easily explore the impact of key parameters (e.g., the load and hopping probabilities) on the network performance. In particular, unlike other models, the expressions we obtain are tractable, i.e., they can be efficiently optimized without extensive simulation. Using these, we optimize the hopping probabilities for the D2D links, i.e., how often they should request a time or frequency slot. This can be viewed as an optimized lower bound to other more sophisticated scheduling schemes. We also investigate the optimal resource partitions between D2D and cellular networks when the dedicated spectrum approach is used.