choujianggailv.js 1.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041
  1. function choujiang(prizes) {
  2. var prizeList = [] //按照权重分解后的奖品数组
  3. prizes.map(function(item){
  4. prizeList.push({
  5. ...item
  6. })
  7. for(var i=0; i< item.gailv; i++) {
  8. prizeList.push({
  9. ...item
  10. })
  11. }
  12. });
  13. prizeList = reset(prizeList);
  14. // 范围随机数
  15. function randomFrom(lowerValue, upperValue) {
  16. return Math.floor(Math.random() * (upperValue - lowerValue + 1) + lowerValue);
  17. };
  18. // 随机打乱数组
  19. function reset(arr) {
  20. var eachArr = arr.concat([])
  21. var lastArr = []
  22. function deepEach(deepArr) {
  23. if (deepArr.length) {
  24. var randomIndex = randomFrom(0, eachArr.length - 1)
  25. lastArr.push(eachArr[randomIndex])
  26. eachArr.splice(randomIndex, 1)
  27. deepEach(eachArr)
  28. }
  29. }
  30. deepEach(eachArr)
  31. return lastArr
  32. }
  33. this.getResult = function() {
  34. var random = randomFrom(0, prizeList.length - 1);
  35. return prizeList[random]
  36. }
  37. }
  38. export default choujiang;