By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
443,965 Members | 1,962 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 443,965 IT Pros & Developers. It's quick & easy.

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

Jory R Ferrell
P: 62
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,104).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.

Expand|Select|Wrap|Line Numbers
  1. x = 1
  2. list1 = []
  3. list_Choices = []
  4.  
  5. list_Choices = input ('Please enter your choices.')
  6. while len(list1) < 90
  7.     if ('1') in list_choices:
  8.         x = random.randint (1,100)
  9.     elif ('12') in list_choices:
  10.         x = random.randint (1,200)
  11.     elif ('23') in list_choices:
  12.         x = random.randint (101,300)
  13.     elif ('123') in list_choices:
  14.         x = random.randint (1,300)
  15.     while x in list1:
  16.          x += 1
  17.          input("Press Enter to continue...")
  18.     while x not in list1:
  19.         list1.append(x)
  20.         print(list1)
  21.     if len(list1) == 90:
  22.       input('Please press any key.')
  23.  



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...
Jul 15 '11 #1

✓ answered by bvdet

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.

Share this Question
Share on Google+
2 Replies


bvdet
Expert Mod 2.5K+
P: 2,851
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.
Jul 15 '11 #2

Jory R Ferrell
P: 62
TY. That was a simple fix. I failed to pay attention to detail and it was driving me up the wall. Again, thank you.
Jul 15 '11 #3

Post your reply

Sign in to post your reply or Sign up for a free account.