Python Forum

Full Version: Logic error when comparing randomly generated integers
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
For a little art prompt generator I'm making, I needed the program to generate random integers corresponding to colours, themes and subjects. For colours, I want it to pick either 1, 2 or 3 colours. For 3 colours I need it to generate three random integers (corresponding to colours in a list) and check them so they're not the same. I used a function to do so:

def fColours3():
   RandColours1 = randint(0,len(Colours2));
   RandColours2 = randint(0,len(Colours2));
   while RandColours1 == RandColours2:
       RandColours2 = randint(0,len(Colours2));
   RandColours3 = randint(0,len(Colours2));
   while RandColours1 == RandColours3 or RandColours3 == RandColours2:
       RandColours3 = randint(0,len(Colours2));
   print (Colours2[RandColours1-1]+",",Colours2[RandColours2-1]+",",Colours2[RandColours3-1]);
   return;
Sometimes it will output something along the lines of "Pink, Pink, Blue" or "Yellow, Red, Yellow" so I can only assume it's not comparing them properly.
I'm a bit new to this programming lark: is there something I'm massively overlooking? Or, do you have an alternative method that's better?

Thanks.

PS. I feel like I should mention that the function checking only two colours works perfectly, as does everything else. The problem may lie elsewhere, though, so here's the rest of the hideously newb-ish code:
from random import randint
def fColours2():
    RandColours1 = randint(0,len(Colours2));
    RandColours2 = randint(0,len(Colours2));
    while RandColours1 == RandColours2:
        RandColours2 = randint(0,len(Colours2));
    print (Colours2[RandColours1-1]+",",Colours2[RandColours2-1]);
    return;

def fColours3():
    RandColours1 = randint(0,len(Colours2));
    RandColours2 = randint(0,len(Colours2));
    while RandColours1 == RandColours2:
        RandColours2 = randint(0,len(Colours2));
    RandColours3 = randint(0,len(Colours2));
    while RandColours1 == RandColours3 or RandColours3 == RandColours2:
        RandColours3 = randint(0,len(Colours2));
    print (Colours2[RandColours1-1]+",",Colours2[RandColours2-1]+",",Colours2[RandColours3-1]);
    return;

Theme = ["Anger","Beauty","Bravery","Celebration","Discovery","Dreams","Exhilaration","Fear","Freedom","Friendship","Heartbreak","Imprisonment","Inhibition","Love","Memory","Motion","Mourning","Novelty","Panic","Regal","Sickness","Smug","Solitude","Sorrow","Stubbornness","Time","Travel","Violence","Visit","Wild","Work"]
Subject = ["Alien","Amphibian","Arthropodian","Avian","Burrowing Creature","Chimera","Dead/Undead","Draconic","Eldritch Abomination (disregard for the laws of the universe)","Air Elemental","Earth Elemental","Fire Elemental","Water Elemental","Fey","Flying Creature","Food Creature","Holy Being","Humanoid","Inanimate Object Creature","Mammal","Mechanical Being","Mythical Being","Piscean (Fishy)","Plant Creature","Prehistoric (non-dinosaur)","Reptilian","Saurian (Dinosaur-like)","Spirit","Supernatural Being (not un-dead or spirit)","Aquatic Creature (non-fish)","Unholy Being"]
Colours = ["Tri-Tone","Two-Tone","Black","White","Grey","Sepia","Red","Yellow","Blue","Green","Orange","Purple","Pink"]
Colours2 = ["Black","White","Grey","Sepia","Red","Yellow","Blue","Green","Orange","Purple","Pink"]

Roll = input("Roll? ")
while Roll != "no" and Roll != "n":
    print(Theme[randint(1,len(Theme))-1]);
    print(Subject[randint(1,len(Subject))-1]);
    #RandColours = randint(1,len(Colours));
    #RandColours = RandColours - 1;
    RandColours = 0;
    if RandColours >= 2:
        print(Colours[RandColours]);
    elif RandColours == 1:
        fColours2();
    else:
        fColours3();
    Roll = input("Roll? ");
print ("Goodbye!");
Why not use sample?

RandColours = random.sample(Colours2, 3)
(Jun-05-2017, 12:43 PM)ichabod801 Wrote: [ -> ]Why not use sample?

RandColours = random.sample(Colours2, 3)

How does sample work? (Do you have a link to some help, or such?) Like I said, I'm very new to this. :)
Naming such a VariableNames ( with capitalized first letter of each word ) is used on classes. It's confusing reading your code.
Use variable_name instead.
https://www.python.org/dev/peps/pep-0008/
(Jun-05-2017, 01:36 PM)SadoDeomeoon Wrote: [ -> ]How does sample work? (Do you have a link to some help, or such?) Like I said, I'm very new to this. :)

The sample function gives you a random sample from a sequence (the first parameter), that is k items long (the second parameter), without replacement (no index from the sequence is chosen more than once). The full documentation is here: https://docs.python.org/3/library/random...dom.sample. Learn to browse the documentation, it is quite good.
Quote:The sample function gives you a random sample from a sequence (the first parameter), that is k items long (the second parameter), without replacement (no index from the sequence is chosen more than once). The full documentation is here: https://docs.python.org/3/library/random...dom.sample. Learn to browse the documentation, it is quite good.

Thank you so much!