473,323 Members | 1,551 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,323 software developers and data experts.

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

Jory R Ferrell
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.

2 1854
bvdet
2,851 Expert Mod 2GB
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
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

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

Similar topics

7
by: The Directive | last post by:
How do you make your code bulletproof? For example, I make my function bulletproof by validating every argument passed to the function. I ensure the arguments are within the expected ranges. This...
3
by: Myron | last post by:
I'm trying to create a query that will tell me which requests took longer than 10 days to move one from particular state to another state. The query I've created returns the correct requests, but...
1
by: Jim Di Griz | last post by:
Hi I have a problem to create an XSD that prevents overlapping ranges. Sample XML: <root> <element LowerLimit="1" UpperLimit="50000"> ..... </element>
4
by: james | last post by:
Hello to all, I have made a calendar from 1900-2000(Except leap years which i do my own!)It gives you the day when inputting the month and date. I want to know how can i improve it in terms of...
16
by: Stefan Wallentowitz | last post by:
Hello together! I'm searching for a statistic about the value ranges of integers in a set of "standard" c-programs. I'm specifically interested in the avarage ratio of assignments etc. of...
6
by: Robin Haswell | last post by:
Hey guys I was wondering if you could give me a hand with something. If I have two tuples that define a range, eg: (10, 20), (15, 30), I need to determine whether the ranges overlap each other....
2
by: Michael Schöller | last post by:
Hi I have some "unusual" problem ^^ Is there an way to enforce the declaration of values? If the answer is no use propertys can anyone tell me how to access a construct like this over...
67
by: PC Datasheet | last post by:
Transaction data is given with date ranges: Beginning End 4/1/06 4/4/06 4/7/06 4/11/06 4/14/06 4/17/06 4/18/06 4/21/06 426/06 ...
0
by: pooky333 | last post by:
Hello! Please help someone... I am still at work and completely stumped. I am a relative beginner at VB and have put together some apparently awful code (as shown below). It is to compare two...
7
by: guido | last post by:
Hi, I'm looking for a container class that can map whole ranges of keys to objects - something like std::map, but not only for individual values for the key, but for whole ranges. Example: I...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.