fork download
  1. # your code goes here# your code goes here
  2.  
  3. import random
  4.  
  5. def CalcNum(s):
  6. num = 0
  7. for i in range(len(items_num)) :
  8. num += items_weight[s][i]*items_num[i]
  9. return num;
  10.  
  11. items_weight = [[0,100,300,1500],[30,100,300,1500]]
  12. items_num = [1,3,3,5]
  13. cnt_items = [0]*12
  14. num_sim = 30000
  15.  
  16. for i in range(num_sim):
  17. items_num = [1,3,3,5]
  18. #print(cnt_items)
  19. flag=0
  20. for j in range(12):
  21. if j<5 :
  22. s=0
  23. else:
  24. s=1
  25. rand = random.randint(1,CalcNum(s))
  26. for k in range(len(items_num)) :
  27. if rand <= items_weight[s][k]*items_num[k] :
  28. items_num[k]-=1
  29. #print("No%d:%d"%(j, k))
  30. if k==0 :
  31. cnt_items[j] +=1
  32. flag=1
  33. break
  34. else:
  35. rand -= items_weight[s][k]*items_num[k]
  36. if rand == 0:
  37. print("error")
  38. exit()
  39. if flag == 1 :
  40. break
  41. print(cnt_items)
  42. for i in range(12):
  43. print("%d: %f"%(i+1, float(cnt_items[i])/num_sim*100))
  44.  
Success #stdin #stdout 1.06s 10576KB
stdin
Standard input is empty
stdout
[0, 0, 0, 0, 0, 402, 738, 1028, 1669, 2758, 4929, 18476]
1: 0.000000
2: 0.000000
3: 0.000000
4: 0.000000
5: 0.000000
6: 1.340000
7: 2.460000
8: 3.426667
9: 5.563333
10: 9.193333
11: 16.430000
12: 61.586667