+ 6

Can random number generator generates a same number continually multiple times?

I am making a game. And in that game I need a loop that will run until rng (random number generator) returns a number that does not match the elements of the array. But one question is bothering me that if rng returns the same number many times? Then my loop will go on for a long time. And there will be a bug in my game. Because no one knows when rng throws what number at you.

12th Oct 2022, 2:08 PM
Commander Erwin Smith
Commander Erwin Smith - avatar
3 Antworten
+ 7
Commander Erwin Smith , > can you please give a sample what range of numers you are using? (e.g. 100 upto 300, 10 numbers required) > using random.sample() is giving unique numbers. > you could also use a list that stores the already used numbers. each time the rng gives you a number, you can check if this number was already used. in this case rng has to generate a new number.
12th Oct 2022, 6:45 PM
Lothar
Lothar - avatar
12th Oct 2022, 2:20 PM
Jayakrishna 🇮🇳
+ 3
If you want to avoid repetition, maybe just create a sequence of all possible values, and sort them in random order. This is also called a "shuffle" algorithm and many languages have built-in functions to randomize a list or array this way. Then you can take as many values from the shuffled list (don't need to use all) and each of them will be unique.
12th Oct 2022, 4:06 PM
Tibor Santa
Tibor Santa - avatar