2024-04-24 20:57:40 发布
网友
我有一个特殊的请求,想调用两个函数f1和f2。我想对95%的总请求调用函数f1,对其余5%的请求调用函数f2。我怎样才能做到这一点
使用随机模块
import random def request_handler(): test = random.random() # random number in [0, 1) if test < 0.95: # 95% of the time, the number will be < 0.95 return f1() return f2() # rest of the time, it'll be higher
使用随机模块
相关问题 更多 >
编程相关推荐