Why is the randint func in my code not generating all values within it's ranges?

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Jory R Ferrell
    New Member
    • Jul 2011
    • 62

    Why is the randint func in my code not generating all values within it's ranges?

    I wrote a small program which asks for seed variables. It adds these vars to a list.That list is used to determine the range which the randint generator can choose from.I then tried to write the program to ask for user input each time a number has been successfully been added to the list and printed.However , I'm running into an issue. Even when the seed variables allow for numbers to be generated between 1 to 400, the list does not generate very many above that 100.In fact, if you take a look at the example of the 2 runs of the program below, you can see the first only generates two numbers above 100, those numbers being 101 and 102, while the second test run generates only four numbers (101,102,103,10 4).So the question is how do I make sure the generator picks accross the spectrum a little more evenly,
    as it seems to repeat this trend with every single run I have performed (about 30) and is obviously not useful if I need random variables throughout greater ranges.Below is an example of the code and below the example is a test run of the program.

    Code:
    x = 1
    list1 = []
    list_Choices = []
    
    list_Choices = input ('Please enter your choices.')
    while len(list1) < 90
        if ('1') in list_choices:
            x = random.randint (1,100)
        elif ('12') in list_choices:
            x = random.randint (1,200)
        elif ('23') in list_choices:
            x = random.randint (101,300)
        elif ('123') in list_choices:
            x = random.randint (1,300)
        while x in list1:
             x += 1
             input("Press Enter to continue...")
        while x not in list1:
            list1.append(x)
            print(list1)
        if len(list1) == 90:
          input('Please press any key.')



    Examples:


    Test Run Example 1
    -----------------------------------------------------------------------------------


    Python 3.2 (r32:88445, Feb 20 2011, 21:30:00) [MSC v.1500 64 bit (AMD64)] on win32
    Type "copyright" , "credits" or "license()" for more information.
    >>> =============== =============== == RESTART =============== =============== ==
    >>>
    Please enter you choices.123

    [60, 15, 20, 61, 55, 52, 79, 24, 64, 56, 26, 62, 90, 63, 54, 70, 45, 29, 86,
    81, 59, 2, 96, 3, 100, 23, 78, 58, 48, 66, 71, 67, 5, 87, 46, 57, 97, 13, 27,
    65, 6, 51, 7, 11, 22, 8, 34, 21, 72, 80, 47, 82, 18, 91, 92, 98, 74, 68, 69,
    73, 88, 75, 76, 25, 16, 49, 33, 28, 77, 95, 99, 83, 38, 53, 4, 84, 85, 17, 89,
    93, 94, 101, 102, 43, 35, 9, 103, 12, 36, 44]
    Press Enter to continue...





    Test Run Example 2
    -----------------------------------------------------------------------------------


    Python 3.2 (r32:88445, Feb 20 2011, 21:30:00) [MSC v.1500 64 bit (AMD64)] on win32
    Type "copyright" , "credits" or "license()" for more information.
    >>> =============== =============== == RESTART =============== =============== ==
    >>>
    Please enter you choices.123

    [60, 5, 24, 61, 87, 48, 62, 71, 95, 96, 88, 63, 15, 9, 37, 14, 86, 89, 8, 100,
    74, 97, 54, 16, 99, 84, 13, 27, 10, 73, 98, 28, 32, 33, 75, 34, 25, 2, 43, 85,
    22, 7, 47, 57, 26, 6, 80, 93, 94, 76, 17, 11, 79, 12, 50, 39, 53, 4, 29, 46, 19,
    18, 21, 30, 41, 20, 40, 23, 31, 78, 101, 77, 81, 35, 64, 52, 90, 82, 42, 102, 36,
    92, 103, 104, 44, 45, 83, 38, 49, 51]
    Press Enter to continue...
  • bvdet
    Recognized Expert Specialist
    • Oct 2006
    • 2851

    #2
    The way you structured your if/elif block, the first choice will always be selected since "1" is in "123". If you want the 4th choice, use if list_choices == '1': etc.

    Comment

    • Jory R Ferrell
      New Member
      • Jul 2011
      • 62

      #3
      TY. That was a simple fix. I failed to pay attention to detail and it was driving me up the wall. Again, thank you.

      Comment

      Working...