Python Forum
Logic error when comparing randomly generated integers
Thread Rating:
  • 1 Vote(s) - 2 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Logic error when comparing randomly generated integers
#1
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!");
Reply
#2
Why not use sample?

RandColours = random.sample(Colours2, 3)
Craig "Ichabod" O'Brien - xenomind.com
I wish you happiness.
Recommended Tutorials: BBCode, functions, classes, text adventures
Reply
#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. :)
Reply
#4
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/
"As they say in Mexico 'dosvidaniya'. That makes two vidaniyas."
https://freedns.afraid.org
Reply
#5
(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.
Craig "Ichabod" O'Brien - xenomind.com
I wish you happiness.
Recommended Tutorials: BBCode, functions, classes, text adventures
Reply
#6
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!
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  pyaudio seems to randomly halt input. elpidiovaldez5 2 311 Jan-22-2024, 09:07 AM
Last Post: elpidiovaldez5
  Logic suggestions for comparing 2 csv's cubangt 7 1,077 Nov-09-2023, 09:54 PM
Last Post: cubangt
  Problem with code / audio is playing randomly, not matching csv requirements Daniel_kcr 2 578 Sep-07-2023, 05:09 PM
Last Post: deanhystad
  HOW TO USE C# GENERATED DLL davide_vergnani 2 1,554 Jun-12-2023, 03:35 PM
Last Post: davide_vergnani
  boto3 - Error - TypeError: string indices must be integers kpatil 7 1,181 Jun-09-2023, 06:56 PM
Last Post: kpatil
  Error "list indices must be integers or slices, not str" dee 2 1,393 Dec-30-2022, 05:38 PM
Last Post: dee
  Python Logic Error rmfooty 3 904 Dec-05-2022, 01:44 PM
Last Post: rmfooty
  how to take a screnshot by Pyautogui automatically and randomly rachidel07 0 3,482 Feb-03-2021, 01:16 PM
Last Post: rachidel07
  list indices must be integers or slices, not lists error djwilson0495 2 2,836 Aug-27-2020, 06:13 PM
Last Post: deanhystad
  comparing integers and fractions tantony 3 1,962 Oct-02-2019, 04:32 PM
Last Post: tantony

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020