Advertisement
Guest User

Adaptations

a guest
Mar 29th, 2017
97
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. data:text/html,<script type="text/javascript">
  2. function checkredundant(adapts,la)
  3. {
  4.  if(la>5)
  5.  {
  6.   return false;
  7.  }
  8.  for(var i=0;i<adapts.length;i++)
  9.  {
  10.   if(adapts[i]==la)
  11.   {
  12.    return true;
  13.   }
  14.  }
  15.  return false;
  16. }
  17.  
  18. function adapt_random(adapts)
  19. {
  20.  var la=Math.floor(Math.random()*10);
  21.  var found=checkredundant(adapts,la);
  22.  adapts.push(la);
  23.  return found;
  24. }
  25.  
  26. function makechoices()
  27. {
  28.  var choices;
  29.  do
  30.  {
  31.   choices=[Math.floor(Math.random()*10),Math.floor(Math.random()*10),Math.floor(Math.random()*10)];
  32.  }
  33.  while(choices[0]===choices[1] || choices[0]===choices[2] || choices[1]===choices[2]);
  34.  return choices;
  35. }
  36.  
  37. function adapt_avoid_random(adapts)
  38. {
  39.  var c=makechoices();
  40.  var b=0;
  41.  if(!checkredundant(adapts,c[0])) {b=0;}
  42.  else if(!checkredundant(adapts,c[1])) {b=1;}
  43.  else if(!checkredundant(adapts,c[2])) {b=2;}
  44.  var found=checkredundant(adapts,c[b]);
  45.  adapts.push(c[b]);
  46.  return found;
  47. }
  48.  
  49. function adapt_avoid(adapts)
  50. {
  51.  var c=makechoices();
  52.  var b=-1;
  53.  if(c[0]>5) {b=0;}
  54.  else if(c[1]>5) {b=1;}
  55.  else if(c[2]>5) {b=2;}
  56.  else
  57.  {
  58.   b=0;
  59.   if(!checkredundant(adapts,c[0])) {b=0;}
  60.   else if(!checkredundant(adapts,c[1])) {b=1;}
  61.   else if(!checkredundant(adapts,c[2])) {b=2;}
  62.  }
  63.  var found=checkredundant(adapts,c[b]);
  64.  adapts.push(c[b]);
  65.  return found;
  66. }
  67.  
  68. function single(n)
  69. {
  70.  var adapts=[];
  71.  while(adapts.length<n)
  72.  {
  73.   if(adapt_avoid(adapts)) /*Either adapt_random(), adapt_avoid_random() or adapt_avoid().*/
  74.   {
  75.    return true;
  76.   }
  77.  }
  78.  return false;
  79. }
  80.  
  81. function go()
  82. {
  83.  var amount=1000000;
  84.  var total=0;
  85.  for(var i=0;i<amount;i++)
  86.  {
  87.   if(single(5))
  88.   {
  89.    total++;
  90.   }
  91.  }
  92.  alert([total," out of ",amount," had redundancies (",(total/amount)," <-> ",(amount/total),")."].join(""));
  93. }
  94.  
  95. go();
  96. </script>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement