I'm having trouble with my lab for a computer class. The problem is as stated:
Design a program that generates 100 random numbers, and keeps a count of how many of those random numbers are even and how many are odd.

Can someone please help me out with this?

Recommended Answers

All 7 Replies

You should try to post some code yourself.

Can give you some tips.

>>> for i in range(0,7):
	a = random.randint(0,100)
	print a
	
79
21
69
60
28
68
0
>>>
>>> 
>>> 1 % 2
1
>>> 2 % 2
0
>>> 3 % 2
1
>>> 4 % 2
0
>>>

Thats the thing, I'm so lost at this point in the class.

One more,now start to think how to put this together.

>>> count = 0
>>> a = 55
>>> if a % 2 == 1:
	count += 1
	print 'odd', count

else:
	print 'even'

	
odd 1
>>>

I understand that part and I've been reading in my book. But I still have yet to figure out how to make it get 100 random numbers and then tell the amount of even and odd numbers.

Try not to over think the problem. You should only need 4 variables in this problem. even, odd, randNum, and i. Variable i is used to control the for loop, randNum is used to hold a random number for even or odd comparison, even and odd are incremented based on the comparison of the randNum. Then print your results to the console. Also what type of computer course are you taking?

dude just combine everything snippsat posted he basically wrote you the programme it's right there, just think about it
create a loop from 1 to 100
generate random number and store in a variable
if the number is odd add 1 to odd variable
else add 1 to even variable
print odd and even variables

Next time if you post here try to post some code.
That will show that you but some effort in to it.

With homework we will not just post finish code.

Here is one soultion,as extra i put number odd/even number in a list.
Try to understand what going on,ask question if you not sure an we break it down.

import random

count_odd = 0
count_even = 0
odd_list = []
even_list = []
for i in range(0,100):
	rand_n = random.randint(0,100)
        if rand_n % 2 == 1:
            count_odd += 1
            odd_list.append(rand_n)
        else:
            count_even += 1
            even_list.append(rand_n)

print 'Out off 100 random number %d was odd\nHere are the numbers->\n' % (count_odd)
print odd_list
print
# Here we do a check that it`s 100 number
print 'Out off %d random number %d was even\nHere are the numbers->\n' % (count_even + count_odd, count_even)
print even_list

'''my output->
Out off 100 random number 44 was odd
Here are the numbers->

[53, 67, 27, 37, 97, 75, 89, 87, 65, 81, 31, 85, 85, 33, 13, 13, 27, 5, 75, 37, 73, 39, 53, 71, 53, 27, 23, 1, 13, 19, 29, 9, 13, 45, 19, 31, 63, 27, 33, 43, 51, 61, 35, 35]

Out off 100 random number 56 was even
Here are the numbers->

[2, 2, 16, 16, 34, 14, 70, 22, 30, 2, 74, 90, 28, 36, 100, 74, 48, 8, 40, 64, 54, 94, 54, 56, 16, 4, 94, 66, 18, 12, 36, 100, 8, 18, 54, 16, 72, 16, 20, 60, 28, 6, 38, 10, 62, 88, 98, 90, 54, 26, 50, 38, 66, 76, 42, 12]
'''
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.