Gestion de personnage et de pool de personnage en javascript vanilla
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

random.js 3.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  1. class RandomGenerator{
  2. constructor( cache ){
  3. this.rng = new Uint8Array( cache );
  4. this.index = cache-- ;
  5. this.generate();
  6. }
  7. generate(){
  8. window.crypto.getRandomValues(this.rng);
  9. this.index = this.rng.length - 2;
  10. return this.rng.length - 1;
  11. }
  12. next(){
  13. return (this.index < 0)? this.generate() : this.index--;
  14. }
  15. rand(){
  16. return this.rng[ this.next() ]/256 ;
  17. }
  18. get( max, min ){
  19. min = ( typeof min === "number")? min : 0 ;
  20. return Math.round(this.rand() * (max - min) + min);
  21. }
  22. roll10( explode ){
  23. explode = ( typeof explode === "number")? differenceToArray( explode, 10 ) : [10];
  24. let dice = this.get( 10, 1 );
  25. return ( explode.indexOf( dice ) >= 0 )? 10 + this.roll10( explode ) : dice ;
  26. }
  27. rk10( r, k, malus, explode ){
  28. let rolls = [];
  29. if((typeof r === "unsigned") || (typeof k === "unsigned"))
  30. return 0;
  31. malus = ( typeof malus === "unsigned")? 0 : malus ;
  32. for(let i = 0 ; i < r ; i++)
  33. rolls[i] = this.roll10( explode );
  34. sort( rolls );
  35. return rolls.slice( 0, k ).reduce((a, b) => a + b) - (malus || 0);
  36. }
  37. }
  38. function differenceToArray( min, max ) {
  39. let result = []
  40. for( let i = min; i <= max; i++)
  41. result.push(i) ;
  42. return result;
  43. }
  44. function sort( array ){
  45. let temp = 0;
  46. var unsorted = true ;
  47. while(unsorted){
  48. unsorted = false;
  49. for(let i = 0; i < array.length -1 ; i++){
  50. if(array[ i ] < array[ i + 1 ]){
  51. temp = array[ i ];
  52. array[ i ] = array[ i + 1 ];
  53. array[ i + 1 ] = temp;
  54. unsorted = true;
  55. }
  56. }
  57. }
  58. }
  59. function sortAssociative( array_arg ){
  60. let result = {};
  61. let array = array_arg;
  62. let higher = 1;
  63. while( !higher ){
  64. higher = 0;
  65. for(let i in array){
  66. higher = (higher)?
  67. (array[higher] < array[i])? i : higher : i ;
  68. if(array[ i ] < array[ i + 1 ]){
  69. temp = array[ i ];
  70. array[ i ] = array[ i + 1 ];
  71. array[ i + 1 ] = temp;
  72. unsorted = true;
  73. }
  74. }
  75. if(higher){
  76. result.push(higher);
  77. delete array[ higher ];
  78. higher = 0;
  79. }
  80. }
  81. return result;
  82. }
  83. function random( max, min ){
  84. min = (typeof min == "undefined")? 0 : min;
  85. return Math.floor(Math.random() * (max - min + 1) + min);
  86. }
  87. function r_gender(){
  88. return (random(1))? "f": "m";
  89. }
  90. function arrayAt( index, array ){
  91. let i = 0;
  92. let result = {};
  93. for(let obj in array){
  94. if( index == ++i ){
  95. result[obj] = array[obj];
  96. return result;
  97. }
  98. }
  99. }
  100. function arrayAtWeight( index, array ){
  101. let i = 0;
  102. for(let obj in array){
  103. if(( i <= index ) && ( index <= (i + array[obj].occurence))){
  104. let rs = {};
  105. rs[ obj ] = array[obj];
  106. return rs;
  107. }
  108. i += array[obj].occurence ;
  109. }
  110. return null;
  111. }
  112. function arrayLength( array ){
  113. let i = 0;
  114. for(let obj in array){
  115. i++;
  116. };
  117. return i;
  118. }
  119. function arrayWeight( array ){
  120. let i = 0;
  121. for(let obj in array){
  122. i += array[obj].occurence;
  123. };
  124. return i;
  125. }
  126. function randomFromAssociative( array ){
  127. return arrayAt( random( arrayLength( array ), 1), array) ;
  128. }
  129. function randomFromArray( array ){
  130. return array[ random( arrayLength( array )) ] ;
  131. }
  132. function randomFromWeight( array ){
  133. return arrayAtWeight( random( arrayWeight( array )), array) ;
  134. }