mercredi 27 juillet 2022

Generate random but always different numbers within a range with CSPRNG

I have a CSPRNG that I'm using to generate 5 numbers between 0 and 20. The problem is, there could be cases where it generates the same number again. So say it generates 5 but running again, it could come out to be 5. If not, then the third or 4th number could be 5.

I want a way to do this where I'm 100% sure the 5 numbers are never the same, even if they do come up as same, I want to re-generate that number in a way that none of the numbers clash.

I thought of using a while loop for each number, to regenerate the number while it's same as the other 4 numbers but that obviously is not optimal and may cause to get stuck in long loops.

Is there a better way?




Aucun commentaire:

Enregistrer un commentaire