# 23834 个钻石，请问最多抽奖多少次？

·  阅读 5529

## 前言

PS：其实这么算的答案不准备，后续我会告诉你最终的答案是 153 次。

## 代码实现

``````
import requests

class Juejin(object):

# 掘金抽奖 URL
lottery_url = "https://api.juejin.cn/growth_api/v1/lottery/draw"

self.session = requests.session()
)
else:

def request(self, *args, **kwargs):

response = self.session.request(*args, **kwargs)
if response.status_code != 200:
raise Exception("Request error")
return response.json()

def draw_lottery(self):
return self.request("post", self.lottery_url)

def lottery():

# session id 自行设置
session_id = ""

domain=".juejin.cn",
name="sessionid",
value=session_id
)

num = 1

while True:
result = juejin.draw_lottery()
if result.get("err_no") == 0:
lottery_name = result.get("data", {}).get("lottery_name")
else:
print(f"第{num}次-抽奖结果为：{lottery_name}")
num += 1
else:
print(result.get("err_msg"))
break

print(f"总抽奖次数为：{num-1}")

print("最终抽奖结果为：")
print(f"礼物：{k} ---- 个数：{v}")

``````第1次-抽奖结果为：66矿石

## 抽奖结果展示

`划重点：此次试验证明掘金抽到 T恤、抱枕、Switch 的概率基本等于零，各位小伙伴不要心存幻想了。`

## 思考与总结

• 如何用程序实现掘金类似的抽奖算法？

``````import random

# 66矿石与Bug的比重为 7:3 [0-700)的随机数为66矿石，[700-1000)为Bug
def draw():
temp = random.randint(0, 1000)

if temp < 700:
return "66矿石"
else:
return "Bug"

• 假设我有 23834 个钻石，请问最多可以抽奖多少次 ？
``````
# 初始化钻石数
num = 23834

# 66钻石获取概率
rate = 0.7

# 抽奖次数
count = 0

while num >= 200:
t, r = divmod(num,200)
count += t
num = (66 * t + r) * rate

print(count)
# 153.0

## JS 版本

``````
stime = Date.parse(new Date());
result = {}
count = 0
flag = true

while (flag) {
obj = await fetch('https://api.juejin.cn/growth_api/v1/lottery/draw', {
method: 'POST',
credentials: "include",
'Content-type': 'application/json; charset=UTF-8'
}
})
.then(res => {
obj = res.json()
return obj

})

if (obj.err_no === 0) {

if (obj.data.lottery_name in result) {
result[obj.data.lottery_name] += 1
} else {
result[obj.data.lottery_name] = 1
}

count ++

} else {
flag = false
}

}
etime = Date.parse(new Date())
console.log("抽奖次数为：" + count)
console.log("抽奖花费时间为：", (etime - stime)/1000 +  " 秒" )
console.log("抽奖奖品明细")
Object.keys(result).forEach(key => console.log("奖品：" + key + "--数量：" + result[key] ))

// 抽奖次数为：3
// 抽奖花费时间为： 1 秒
// 抽奖奖品明细
// 奖品：66矿石--数量：2