Advertisement
Guest User

Untitled

a guest
Aug 20th, 2019
78
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.24 KB | None | 0 0
  1. var randomizer = function () {
  2. return Math.random() > 0.02;
  3. };
  4.  
  5. var randomizer = {
  6. _ratio: 0.02, // default ratio
  7. setRatio: function (falses, total) {
  8. this._ratio = falses / total;
  9. },
  10. getResult: function () {
  11. return Math.random() > this._ratio;
  12. }
  13. };
  14.  
  15. randomizer.getResult();
  16. randomizer.setRatio(60, 1000); // 60 out of 10000
  17.  
  18. function mostlyFalse() {
  19. return Math.random() <= 0.05;
  20. }
  21.  
  22. //Get random number from 1 to 20, if equals 20 return false
  23. if (Math.floor((Math.random()*20)+1) == 20)
  24. {
  25. return false;
  26. }
  27. else{
  28. return true;
  29. }
  30.  
  31. function suchRandom(chanceInPercent){
  32. return Math.random() > chanceInPercent/100;
  33. }
  34.  
  35. console.log(suchRandom(2)); //because 1000/50 = 20 true / 1000 calls = 2%
  36.  
  37. function foo () { return Math.random() > 0.02; }
  38.  
  39. // 1 / 50 == 0.02, so if it's less than 0.02 return false
  40.  
  41. var num = Math.floor((Math.random()*50)+1);
  42.  
  43. function chance( c = 0.5 ){
  44. return Math.random() < c;
  45. }
  46.  
  47. // Block has a 60% chance of being executed.
  48. if( chance( .6 ) ){
  49. //Do stuff
  50. }
  51.  
  52. // Or, to solve your problem specifically
  53. var c = chance( 49 / 50 );
  54.  
  55. var c = 0;
  56. function f()
  57. {
  58. return ++c % 50 == 0 ? false : true;
  59. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement