Functions.ts 858 B

123456789101112131415161718192021222324252627282930313233343536373839
  1. /**
  2. * Makes the array a have unique elements.
  3. * WARNING: WILL ALTER THE ARRAY GIVEN
  4. * @param a
  5. */
  6. function arrayUnique(a : Array<any>) {
  7. for(var i=0; i<a.length; ++i) {
  8. for(var j=i+1; j<a.length; ++j) {
  9. if(a[i] === a[j])
  10. a.splice(j--, 1);
  11. }
  12. }
  13. return a;
  14. }
  15. function arrayUniqueNewArray(a : Array<any>) {
  16. a = a.slice();
  17. arrayUnique(a);
  18. return a;
  19. }
  20. function arrayShuffle (a : Array<any>) {
  21. for (let i = 0; i < a.length; i++) {
  22. let pos = Math.floor(Math.random() * (a.length - i)) + i;
  23. let temp = a[i];
  24. a[i] = a[pos];
  25. a[pos] = temp;
  26. }
  27. }
  28. function arrayShuffleNewArray (a : Array<any>) {
  29. let nA = a.slice();
  30. arrayShuffle(nA);
  31. return nA;
  32. }
  33. function randomRange (min, max) {
  34. return Math.floor(Math.random() * (max - min + 1)) + min;;
  35. }