mercredi 18 janvier 2017

Random number generation from 1 to 7

I was going through Google Interview Questions. to implement the random number generation from 1 to 7. I did write a simple code, I would like to understand if in the interview this question asked to me and if I write the below code is it Acceptable or not?

import time
def generate_rand():
    ret = str(time.time()) # time in second like, 12345.1234
    ret = int(ret[-1])
    if ret == 0 or ret == 1:
        return 1
    elif ret > 7:
        ret = ret - 7
        return ret
    return ret

while 1:
    print(generate_rand())
    time.sleep(1)




Aucun commentaire:

Enregistrer un commentaire