javascript - Fisher Yates shuffle 1 number shown -


i using fisher yates shuffle , gone through little tutorial, can see can randomize order such 4,1,3,2,6,7,5 etc... want find out how have 1 number shown. there submit button , when press submit random number between 1 7 shown. number put aside when hit submit again there 100% chance wont see number again. can press submit 7 times , 8th time nothing happen. javascript using:

    <script> array.prototype.shuffle = function(){   var = this.length, j, temp; while(--i > 0) {     j = math.floor(math.random() * (i+1));     temp = this[j];     this[j] = this[i];     this[i] = temp;     }     return this; } var arr = ['a','b','c','d','e','f','g','h']; var result = arr.shuffle();  document.write(result); </script> 

i have googled no success, many thanks.

here implementation of have used.

function fisheryates ( myarray,stop_count ) {   var = myarray.length;   if ( == 0 ) return false;   var c = 0;   while ( --i ) {      var j = math.floor( math.random() * ( + 1 ) );      var tempi = myarray[i];      var tempj = myarray[j];      myarray[i] = tempj;      myarray[j] = tempi;      c++;      if(c == stop_count)return;    } } 

you can call :

fisheryates(arr,math.min(nr_items, selectionsize)); 

note that

 var tempi = myarray[i];  var tempj = myarray[j];  myarray[i] = tempj;  myarray[j] = tempi; 

is dumber version of:

 var temp = myarray[i];  myarray[i] = myarray[j];  myarray[j] = temp; 

Comments

Popular posts from this blog

Android : Making Listview full screen -

javascript - Parse JSON from the body of the POST -

javascript - Chrome Extension: Interacting with iframe embedded within popup -