Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- data:text/html,<script type="text/javascript">
- function checkredundant(adapts,la)
- {
- if(la>5)
- {
- return false;
- }
- for(var i=0;i<adapts.length;i++)
- {
- if(adapts[i]==la)
- {
- return true;
- }
- }
- return false;
- }
- function adapt_random(adapts)
- {
- var la=Math.floor(Math.random()*10);
- var found=checkredundant(adapts,la);
- adapts.push(la);
- return found;
- }
- function makechoices()
- {
- var choices;
- do
- {
- choices=[Math.floor(Math.random()*10),Math.floor(Math.random()*10),Math.floor(Math.random()*10)];
- }
- while(choices[0]===choices[1] || choices[0]===choices[2] || choices[1]===choices[2]);
- return choices;
- }
- function adapt_avoid_random(adapts)
- {
- var c=makechoices();
- var b=0;
- if(!checkredundant(adapts,c[0])) {b=0;}
- else if(!checkredundant(adapts,c[1])) {b=1;}
- else if(!checkredundant(adapts,c[2])) {b=2;}
- var found=checkredundant(adapts,c[b]);
- adapts.push(c[b]);
- return found;
- }
- function adapt_avoid(adapts)
- {
- var c=makechoices();
- var b=-1;
- if(c[0]>5) {b=0;}
- else if(c[1]>5) {b=1;}
- else if(c[2]>5) {b=2;}
- else
- {
- b=0;
- if(!checkredundant(adapts,c[0])) {b=0;}
- else if(!checkredundant(adapts,c[1])) {b=1;}
- else if(!checkredundant(adapts,c[2])) {b=2;}
- }
- var found=checkredundant(adapts,c[b]);
- adapts.push(c[b]);
- return found;
- }
- function single(n)
- {
- var adapts=[];
- while(adapts.length<n)
- {
- if(adapt_avoid(adapts)) /*Either adapt_random(), adapt_avoid_random() or adapt_avoid().*/
- {
- return true;
- }
- }
- return false;
- }
- function go()
- {
- var amount=1000000;
- var total=0;
- for(var i=0;i<amount;i++)
- {
- if(single(5))
- {
- total++;
- }
- }
- alert([total," out of ",amount," had redundancies (",(total/amount)," <-> ",(amount/total),")."].join(""));
- }
- go();
- </script>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement