Many saints, while they were alive, performed miracles. But how did they know the miracle would work before they did it? For example, when St. Anthony presented to a donkey the Blessed Eucharist and some other food, how did he know the donkey would bow to the Blessed Eucharist?
An even better example would be when St. Vincent Ferrer was giving a homily at a funeral and commanded the corpse to confirm that they were in the end times. According to the story, the corpse sat up in the coffin, said, “Yes, Father!” and fell back into the coffin, dead.
The answer is that the saints did not “know” beforehand that they would perform a miracle and did not necessarily set out to perform a miracle. They simply had faith in God that God would show his favor. In the case of St. Vincent’s command to the corpse, he may have been so caught up in his preaching that he did not even think through his command to the corpse, but just acted in the moment and God granted his request.