Non repeated random numbers | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
0

Non repeated random numbers

is there a way to randomize random number generation without repeating an already generated number?

12th Oct 2016, 9:35 AM
Fhatuwani Luvhengo
Fhatuwani Luvhengo - avatar
2 Answers
+ 1
You can use a list to store the numbers that already present and create an if statement if the new random number is present in that list.
12th Oct 2016, 9:42 AM
Rene Christian Tio
Rene Christian Tio - avatar
+ 1
You can track unique values with sets. A set does not allow duplicate values. You could then wrap this in a generator function so that it looks like all you ever return is unique values. Note that you are breaking the very principle of randomness, a 'bad idea' in some contexts: http://security.stackexchange.com/questions/2202/lessons-learned-and-misconceptions-regarding-encryption-and-cryptology/2210#2210
12th Oct 2016, 4:29 PM
Kirk Schafer
Kirk Schafer - avatar