We consider the problem of maximizing the probability of stopping on any of the last m successes in independent Bernoulli trials with random horizon of length N, where m is a predetermined integer. A prior is given for N. It is known that, when N is degenerate, i.e. P{N = n} = 1 for a given n > m, the sum-the-multiplicative-odds theorem gives the solution and shows that the optimal rule is a threshold rule, i.e. it stops on the first success appearing after a given stage. However, when N is nondegenerate, the optimal rule is not necessarily a threshold rule. So our main concern in Section 2 is to give a sufficient condition for the optimal rule to be a threshold rule when N is a bounded random variable such that P{N ≤ n} = 1. Application will be made to the usual (discrete arrival time) secretary problem with a random number N of applicants in Section 3. When N is uniform or curtailed geometric, the optimal rules are shown to be threshold rules and their asymptotic results are obtained. We also examine, as a nonhomogeneous Poisson process model, an intermediate prior that allows N to be uniform or degenerate. In Section 4 we consider a continuous arrival time version of the secretary problem with a random number M of applicants. It is shown that, whatever the distribution of M, we can win with probability greater than or equal to um*, where um* is, as given in (1.4), the asymptotic win probability of the usual secretary problem when N degenerates to n and n → ∞.