Sandstone is an important carrier of underground hydrocarbon and the geological sequestration of carbon dioxide. Primary porosity is an important parameter used to predict reservoir quality, and it is influenced by the grain shape, grain‐size distribution and grain packing texture. However, few studies have focused on deriving multivariate equations of the grain size, grain‐size distribution and grain packing texture to predict the primary porosity of sandstone. The natural sedimentary process of sandstones was designed and simulated using a discrete element method. The results confirmed that the primary porosity is influenced by roundness, flatness, elongation, grain‐size distribution and grain packing texture; of the attributes, the mean grain size contributes less to the primary porosity of sandstone. Based on 193 simulated sandstone samples with a one‐component grain packing texture, a mathematical model considering roundness, flatness, elongation, grain packing texture and grain‐size distribution was proposed. The maximum, mean absolute and root mean square errors were 0.71, 0.26 and 0.32, respectively. The correlation coefficient between the simulated results and mathematical predicted results was 0.94. Furthermore, the primary porosity was positively correlated with the kurtosis of the grain‐size distribution in sandstone with the same grain shape but different kurtosis. The primary porosity was found to be independent of the mean grain size and mainly affected by the grain packing texture. Moreover, the primary porosity prediction model for sandstones with different grain‐size distribution curves based on the grain packing texture, which was proposed by the author in a previous study, was verified, and the correlation coefficient between the simulated results and predicted results was >0.92. The new mathematical model proposed in this study is a useful supplement for key parameter acquisition in the previous primary porosity prediction model of sandstone with different grain‐size distribution curves and grain packing texture. A mathematical model considering roundness, flatness, elongation, grain packing texture and grain‐size distribution is of great significance for reservoir quality prediction and conducting a quantitative evaluation of the diagenesis.