dimanche 18 octobre 2015

How to produce a set of random numbers based on another randomly selected range in python

I need to write a program that generates a random integer named file_size between 4 and 7, inclusive, then print them, but each random integer must be both between 5 and 19 inclusive and be an odd number. Duplicates are okay.To summarize, if file_size is 6, then 6 odd numbers ranging from 5 to 19 must be printed. I'm using Python 3 by the way. This is what I have so far:

import random

file_size = random.randint(4, 7)

print("file_size = ", file_size)

for _ in range(file_size):
   random_num = random.randint(5, 19)
   if random_num % 2 ==1:
       print(random_num)

Output is as follows for a sample run:

file_size =  5
19
11
15
7

This obviously isn't what I'm looking for. Please help.




Aucun commentaire:

Enregistrer un commentaire