mardi 6 décembre 2016

Phyton random.randint always producing exactly half even half odd vales

I am doing some practice exercises learning Python and have noticed the code i wrote below always products exactly the same amount of odd and even values no matter how many times i run it.

It is producing different list values, so is it truly random?

Can anyone explain if this is normal or a way to combat it?

Output :

[834, 7719, 2279, 7403, 241, 7198, 4278, 2339, 7492, 928, 1372, 226, 8134, 165, 3999, 5367, 8807, 5838, 9637, 5850, 2945, 6419, 5131, 1597, 775, 2493, 518, 9175, 5980, 7705, 1259, 7884, 7220, 7746, 2144, 7542, 5939, 3435, 2723, 7887, 3978, 1483, 3709, 7447, 416, 5977, 303, 9011, 5707, 3085, 41, 1441, 4119, 677, 1048, 4399, 5737, 9032, 5629, 2953, 8979, 4165, 6820, 1830, 7193, 5760, 7446, 6383, 2937, 9591, 6700, 9505, 4896, 2843, 5994, 5420, 5063, 1480, 9187, 5160, 3191, 1715, 324, 8124, 5275, 7265, 5757, 8441, 4573, 9946, 8735, 3345, 4823, 7777, 7555, 8140, 4801, 7034, 1074, 8325]

Odd is 50

Even is 50

Code:

import random

numbers = []
even = 0
odd = 0

for x in range(100):
    numbers = numbers + [random.randint(1,9999)]

for y in range(len(numbers)):
    if int(y) % 2 == 0 :
        even = even + 1    
    elif int(y) % 2 == 1:
        odd = odd + 1

print(str(numbers))
print('Odd is ' + str(odd))
print('Even is ' + str(even))




Aucun commentaire:

Enregistrer un commentaire