RandomUtils.js 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  1. var t = require;
  2. var e = module;
  3. var i = exports;
  4. Object.defineProperty(i, "__esModule", {value: !0}),
  5. (i.default = class {
  6. limit(t, e) {
  7. e -= t;
  8. return t + Math.random() * e;
  9. }
  10. limitInteger(t, e) {
  11. return Math.floor(this.limit(t, e + 1));
  12. }
  13. randomArray(t) {
  14. return t[Math.floor(Math.random() * t.length)];
  15. }
  16. shuffle(e) {
  17. for (let t = e.length - 1; 0 <= t; t--) {
  18. var i = Math.floor(Math.random() * (t + 1)),
  19. s = e[i];
  20. (e[i] = e[t]), (e[t] = s);
  21. }
  22. return e;
  23. }
  24. randomArrayByWeight(e, i) {
  25. var s = e.length;
  26. if (!i) for (let t = (i = 0); t < s; t++) i += e[t].weight;
  27. let o = Math.random() * i;
  28. for (let t = 0; t < s; t++) if ((o -= e[t].weight) <= 0) return e[t];
  29. }
  30. judgeSpecialRandom(t, e) {
  31. e = s.P2C(e);
  32. return Math.random() < e * (t + 1);
  33. }
  34. });
  35. var n = {};
  36. class s {
  37. constructor(t, e) {
  38. (this.times = e = void 0 === e ? 0 : e), (this.c = 0), (this.c = s.P2C(t));
  39. }
  40. draw() {
  41. this.times++;
  42. var t = Math.random() < this.times * this.c;
  43. return t && (this.times = 0), t;
  44. }
  45. static P2C(t) {
  46. if (n[t]) return n[t];
  47. if (t <= 0) return 0;
  48. for (var e = 0, i = 0, s = t, o = 0; ; ) {
  49. var a = this.C2P((o = (s + e) / 2));
  50. if (Math.abs(a - i) <= 1e-14) break;
  51. t < a ? (s = o) : (e = o), (i = a);
  52. }
  53. return (n[t] = o);
  54. }
  55. static C2P(t) {
  56. for (var e, i = 0, s = 0, o = Math.ceil(1 / t), a = 1; a <= o; a++)
  57. (s += e = (1 - s) * Math.min(1, t * a)), (i += e * a);
  58. return 1 / i;
  59. }
  60. }