Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

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.

>Solution :

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

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.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading