简体   繁体   中英

strange behavior of python function time.time()

consider the following python function:

def prandom():
    t = time.time()
    s = t - int(t)
    s = str('{0:.16f}'.format(s))
    print s
    s = int(s[5])
    print(s)
    return s

the naive intention is to get a pseudorandom digit. However: how do you explain that, no matter how many times I run the function (on a Windows 10 machine) I get either a 0 or a 9? Is there something I am missing?

Some modification in your code better modify your function:

def prandom():
    t = time.time()
    return str("{0:.16f}".format(t))[-1]

This will give all the values

Best way to generate random number is to use random function

import random

a = random.randint(0, 9) # two argument will generate number between those numbers
print a # random digit from 0 to 9

OUTPUT :

8

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM