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

sql - VB.NET Operand type clash: date is incompatible with int error -

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

python - TypeError: Scalar value for argument 'color' is not numeric in openCV -