473,511 Members | 15,081 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

processing a random.sample

1 New Member
I am pretty new to Python. I wanted to create a program so that if a user entered a number, then that amount of random numbers will appear.

I wanted my random.randrange(100)+1

"How many numbers do you want to generate?" 4
19
2
45
35

Also I wanted to know how to find the
- average to the random numbers
- tell which ones are even
- tell which ones are odd, divisible by3, and greater than 50

Can anyone PLEASE help me to any part of this? THank you so much!
Mar 8 '07 #1
1 1562
bartonc
6,596 Recognized Expert Expert
I am pretty new to Python. I wanted to create a program so that if a user entered a number, then that amount of random numbers will appear.

I wanted my random.randrange(100)+1

"How many numbers do you want to generate?" 4
19
2
45
35

Also I wanted to know how to find the
- average to the random numbers
- tell which ones are even
- tell which ones are odd, divisible by3, and greater than 50

Can anyone PLEASE help me to any part of this? THank you so much!
To get the input, use:
Expand|Select|Wrap|Line Numbers
  1. countStr = raw_input("How many numbers do you want to generate?")
  2. try:
  3.     count = int(countStr)
  4. except ValueError:
  5.     count = 0
To get a list of random numbers between 0 and 99:
Expand|Select|Wrap|Line Numbers
  1. import random
  2. randList = random.sample(range(100), count)
To get the average:
Expand|Select|Wrap|Line Numbers
  1. avg = sum(randList) / len(randList)
To count the number of 'things':
Expand|Select|Wrap|Line Numbers
  1. i = 0
  2. for item in randList:
  3.     if item > 50:
  4.         i += 1
  5. print "items greater than 50 =", i
Let's see if you can do some on your own. There's help here if you need it.
Mar 8 '07 #2

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

Similar topics

4
2945
by: Bart Nessux | last post by:
New to Python... trying to figure out how to count the objects in a list and then map the count to the objects or convert the list to a dict... I think the latter would be better as I need a number...
9
2248
by: Bart Nessux | last post by:
I am using method 'a' below to pick 25 names from a pool of 225. A co-worker is using method 'b' by running it 25 times and throwing out the winning name (names are associated with numbers) after...
21
23094
by: Andreas Lobinger | last post by:
Aloha, i wanted to ask another problem, but as i started to build an example... How to generate (memory and time)-efficient a string containing random characters? I have never worked with...
4
3396
by: james blair | last post by:
Hi I am generating a random number using random.randint(1,10000000000) Whats the possibility that the numbers generated will be same when generated by 100 users at the same time? Whats the best...
1
4707
by: steflhermitte | last post by:
Dear cpp-ians, I want to apply a stratified sampling on an image. Following simplified example will explain my problem. The original image I with nrows and ncols is now a vector V of length...
4
5088
by: Jesse Noller | last post by:
Hello - I'm probably missing something here, but I have a problem where I am populating a list of lists like this: list1 = list2 = list3 = main_list =
4
6630
by: Jonathan Burd | last post by:
Greetings everyone, Here is a random string generator I wrote for an application and I'm wondering about the thread-safety of this function. I was told using static and global variables cause...
5
2260
by: jordi | last post by:
I need the random.sample functionality where the population grows up to long int items. Do you know how could I get this same functionality in another way? thanks in advance. Jordi
5
1908
by: B. Williams | last post by:
I need some assistance with random access file processing. I have a function that I would like to change from sequential file processing to random access. Thanks in advance. void...
0
7252
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
7432
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
1
7093
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
7517
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
5676
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
3230
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
3218
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
1583
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
0
452
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

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.