e0b7d91d3a241f7bfaf72a418694cc2ef48cfbc9.js 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178
  1. System.register(["cc"], function (_export, _context) {
  2. "use strict";
  3. var _cclegacy, RandomManager, _crd;
  4. _export("RandomManager", void 0);
  5. return {
  6. setters: [function (_cc) {
  7. _cclegacy = _cc.cclegacy;
  8. }],
  9. execute: function () {
  10. _crd = true;
  11. _cclegacy._RF.push({}, "3e09eJBwfZBsLPqFszZLvMS", "RandomManager", undefined);
  12. /** 引擎 utils.ts 中有一些基础数学方法 */
  13. /**
  14. * 随机管理
  15. * @help https://gitee.com/dgflash/oops-framework/wikis/pages?sort_id=12037911&doc_id=2873565
  16. */
  17. _export("RandomManager", RandomManager = class RandomManager {
  18. constructor() {
  19. this.random = null;
  20. }
  21. /** 随机数管理单例对象 */
  22. static get instance() {
  23. if (this._instance == null) {
  24. this._instance = new RandomManager();
  25. this._instance.setRandom(Math.random);
  26. }
  27. return this._instance;
  28. }
  29. /** 设置第三方随机库 */
  30. setRandom(random) {
  31. this.random = random;
  32. }
  33. getRandom() {
  34. return this.random();
  35. }
  36. /**
  37. * 生成指定范围的随机浮点数
  38. * @param min 最小值
  39. * @param max 最大值
  40. */
  41. getRandomFloat(min = 0, max = 1) {
  42. return this.getRandom() * (max - min) + min;
  43. }
  44. /**
  45. * 生成指定范围的随机整数
  46. * @param min 最小值
  47. * @param max 最大值
  48. * @param type 类型
  49. * @example
  50. var min = 1;
  51. var max = 10;
  52. // [min,max) 得到一个两数之间的随机整数,这个值不小于min(如果min不是整数的话,得到一个向上取整的 min),并且小于(但不等于)max
  53. RandomManager.instance.getRandomInt(min, max, 1);
  54. // [min,max] 得到一个两数之间的随机整数,包括两个数在内,这个值比min大(如果min不是整数,那就不小于比min大的整数),但小于(但不等于)max
  55. RandomManager.instance.getRandomInt(min, max, 2);
  56. // (min,max) 得到一个两数之间的随机整数
  57. RandomManager.instance.getRandomInt(min, max, 3);
  58. */
  59. getRandomInt(min, max, type = 2) {
  60. min = Math.ceil(min);
  61. max = Math.floor(max);
  62. switch (type) {
  63. case 1:
  64. // [min,max) 得到一个两数之间的随机整数,这个值不小于min(如果min不是整数的话,得到一个向上取整的 min),并且小于(但不等于)max
  65. return Math.floor(this.getRandom() * (max - min)) + min;
  66. case 2:
  67. // [min,max] 得到一个两数之间的随机整数,包括两个数在内,这个值比min大(如果min不是整数,那就不小于比min大的整数),但小于(但不等于)max
  68. return Math.floor(this.getRandom() * (max - min + 1)) + min;
  69. case 3:
  70. // (min,max) 得到一个两数之间的随机整数
  71. return Math.floor(this.getRandom() * (max - min - 1)) + min + 1;
  72. }
  73. return 0;
  74. }
  75. /**
  76. * 根据最大值,最小值范围生成随机数数组
  77. * @param min 最小值
  78. * @param max 最大值
  79. * @param n 随机个数
  80. * @example
  81. var a = RandomManager.instance.getRandomByMinMaxList(50, 100, 5)
  82. console.log("随机的数字", a);
  83. */
  84. getRandomByMinMaxList(min, max, n) {
  85. var result = [];
  86. for (let i = 0; i < n; i++) {
  87. result.push(this.getRandomInt(min, max));
  88. }
  89. return result;
  90. }
  91. /**
  92. * 获取数组中随机对象
  93. * @param objects 对象数组
  94. * @param n 随机个数
  95. * @example
  96. var b = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10];
  97. var r = RandomManager.instance.getRandomByObjectList(b, 5);
  98. console.log("原始的对象", b);
  99. console.log("随机的对象", r);
  100. */
  101. getRandomByObjectList(objects, n) {
  102. var temp = objects.slice();
  103. var result = [];
  104. for (let i = 0; i < n; i++) {
  105. let index = this.getRandomInt(0, temp.length, 1);
  106. result.push(temp.splice(index, 1)[0]);
  107. }
  108. return result;
  109. }
  110. /**
  111. * 定和随机分配
  112. * @param n 随机数量
  113. * @param sum 随机元素合
  114. * @example
  115. var c = RandomManager.instance.getRandomBySumList(5, -100);
  116. console.log("定和随机分配", c);
  117. */
  118. getRandomBySumList(n, sum) {
  119. let residue = sum;
  120. let value = 0;
  121. const result = [];
  122. for (let i = 0; i < n; i++) {
  123. value = this.getRandomInt(0, residue, 3);
  124. if (i == n - 1) {
  125. value = residue;
  126. } else {
  127. residue -= value;
  128. }
  129. result.push(value);
  130. }
  131. return result;
  132. }
  133. });
  134. RandomManager._instance = void 0;
  135. _cclegacy._RF.pop();
  136. _crd = false;
  137. }
  138. };
  139. });
  140. //# sourceMappingURL=e0b7d91d3a241f7bfaf72a418694cc2ef48cfbc9.js.map