Skip to content
Advertisement

Can I randomly generate numbers without the same number next to eachother?

My goal is to generate a series of random numbers without allowing the same number repeating next to eachother but allowing the same number twice like 1,2,1,2 but not 1,1,2,2 and I’m just not really sure how to accomplish this.

Advertisement

Answer

Something like this?

import random
list = []
list.append(random.randrange(50))
for i in range(50):
    x = random.randrange(50)
    while x == list[i]:
        x = random.randrange(50)
    list.append(x)
print(list)

Also you should post your own attempt. It gives everyone a good reference and starting point to help you out in a meaning full and focused way.

User contributions licensed under: CC BY-SA
1 People found this is helpful
Advertisement