jquery - Random Pick from Array without duplicate -


i need pick element array 10 different times without picking duplicates in process:

function raffle(){    question_pool = array(a,b,c,d,e,f,g,h,i,j,k,l,m);    question = question_pool[math.floor(math.random()*question_pool.length)]; } 

this function raflle(), when execute it, may result twice.

here approach:

question_pool = array(a,b,c,d,e,f,g,h,i,j,k,l,m); maximum = 10; minimum = 0; question_stack = math.floor(math.random() * question_pool.length - minimum); minimum = math.min(minimum + 1, maximum); question = question_pool.splice(question_stack,1); question_pool.push(question); 

use splice remove selected questions question pool. once questions have been drawn continue.

var question_pool = array("a","b","c","d","e","f","g","h","i","j","k","l","m"); do{   var id = math.floor(math.random() * question_pool.length);;   var question = question_pool[id];   question_pool.splice(id, 1);   console.log(question); }while(question_pool.length > 0); 

https://jsfiddle.net/nc67chwn/

subsequently, create deep clone of question_pool array , splice elements if wanted preserve original question group.


Comments

Popular posts from this blog

SVG stroke-linecap doesn't work for circles in Firefox? -

routes - Laravel 4 Wildcard Routing to Different Controllers -

cross browser - XSLT namespace-alias Not Working in Firefox or Chrome -