In some clinical settings such as the cancer immunotherapy trials, a treatment time-lag effect may be present and the lag duration possibly vary from subject to subject. An efficient study design should not only take into account the time-lag effect but also consider the individual heterogeneity in the lag duration. In this paper, we present a Generalized Piecewise Weighted Log-rank test, designed to account for the random time-lag effect while maximizing the study power with respect to the weights. Based on the proposed test, both analytic and numeric approaches are developed for the sample size and power calculation. Asymptotic properties are derived and finite sample efficiency is evaluated in simulations. Compared to the standard practice ignoring the delayed effect, the proposed design and analysis procedures are substantially more efficient when a random lag is expected; further, compared to the existing methods by Xu et al. (2017) considering the fixed time-lag effect, the proposed approaches are significantly more robust when the lag model is mis-specified. An implementation in an R package(DelayedEffect.Design) is provided.