JS implementation 1, 2, 3, 5 numbers are generated according to probability

JS is generated according to the probability of the configuration, the probability rules are as follows:
1 ———— 50%

2 ———– -30%

3 ———— 15%

5 ———— 5%

Simple code

Function myrandom () {var rand = math.random (); if (Rand
  < .5) return 1;
 if (rand < .8) return 2;
 if (rand < .95) return 3;
 return 5;
} 

   
Function prizrand (OARR) {var sum = 0; // sum VAR RAND = 0; // The random number Var Result generated each time the cycle is. 0; // Return to the key constole.log (OARR); // Computing total and for (VAR i IN OARR) {SUM + = OARR [i] [0];} // thinking is if the set number is random The number is returned, otherwise the number of for this time for (VAR I IN OARR) {Rand = Math.Floor (Math.random () * Sum + 1); if (Oarr [i] [0]> = Rand ) {result = OARR [I] [0]; Break;} else {sum – = OARR [i] [0];}} return result;} var Oarr = {‘5’: [5, ‘Mac’], ‘3’: [15, ‘iPhone’], ‘2’: [30, ‘ipad’], ‘1’: [50, ‘iWatch’]}; console.logPriZERAND (OARR));

You can choose from to see the TUMI cloud as needed.
© Copyright Notice
THE END
Just support it if you like
like0
share
comment Grab the couch

Please log in to comment