Skip to content Skip to sidebar Skip to footer

Javascript Random On Array Without Repeat

So I'm trying to randomize the questions but i kept on failing. i tried to use this code to add but there are some issues with it. i changed currentQuestion to randomQuiz and it wo

Solution 1:

Just shuffle the question array and pick one after the other. In a simplified version:

var questions = [1,2,3,4,5,6,7,8,9,10];

functionshuffle(a) {
  var cidx, ridx,tmp;
  cidx = a.length;
  while (cidx != 0) {
    ridx = Math.floor(Math.random() * cidx);
    cidx--;
    tmp = a[cidx];
    a[cidx] = a[ridx];
    a[ridx] = tmp;
  }
  return a;
}

function* get_one(arr){
  var idx = arr.length;
  while(idx != 0)
    yield arr[idx--];
}

questions = shuffle(questions);
console.log(questions);

var nextq = get_one(questions);
var alen = questions.length -1;
while(alen--)
  console.log(nextq.next().value);

You don't need that fancy generator, you can just get one after the other in a simple loop if you prefer that.

Solution 2:

Depending on what you're doing at the end of the quiz, the easiest thing might be to just remove the question from the array once it's been answered.

Solution 3:

I have another very small solution:

It copies the array of possible values and removes the ones, that are added to the new array.

functionpickFrom(n, list) {
  const copy = Array.from(list);
  returnArray.from(Array(n), () => copy.splice(Math.floor(copy.length * Math.random()), 1)[0]);
}

var quiz = [{
  "question": "What is the full form of IP?",
  "choices": ["Internet Provider", "Internet Port", "Internet Protocol"],
  "correct": "Internet Protocol"
}, {
  "question": "Who is the founder of Microsoft?",
  "choices": ["Bill Gates", "Steve Jobs", "Steve Wozniak"],
  "correct": "Bill Gates"
}, {
  "question": "1 byte = ?",
  "choices": ["8 bits", "64 bits", "1024 bits"],
  "correct": "8 bits"
}, {
  "question": "The C programming language was developed by?",
  "choices": ["Brendan Eich", "Dennis Ritchie", "Guido van Rossum"],
  "correct": "Dennis Ritchie"
}, {
  "question": "What does CC mean in emails?",
  "choices": ["Carbon Copy", "Creative Commons", "other"],
  "correct": "Carbon Copy"
}, {
  "question": "wsxwsxwsxwsxwsxwsx?",
  "choices": ["wsx", "edc", "qaz"],
  "correct": "wsx"
}, {
  "question": "qazqazqazqazqazqaz?",
  "choices": ["qaz", "wsx", "edc"],
  "correct": "qaz"
}, {
  "question": "asdasdasdasdasdasd?",
  "choices": ["asd", "qwe", "zxc"],
  "correct": "asd"
}, {
  "question": "zxczxczxczxczxczxc?",
  "choices": ["zxc", "asd", "qwe"],
  "correct": "zxc"
}, {
  "question": "qweqweqweqweqweqwe?",
  "choices": ["qwe", "asd", "zxc"],
  "correct": "qwe"
}];

console.log(pickFrom(3, quiz));

Reduced example:

functionpickFrom(n, list) {
  const copy = Array.from(list);
  returnArray.from(Array(n), () => copy.splice(Math.floor(copy.length * Math.random()), 1)[0]);
}

list = [1, 2, 3];

console.log("Pick 1:", pickFrom(1, list).toString());
console.log("Pick 2:", pickFrom(2, list).toString());
console.log("Pick 3:", pickFrom(3, list).toString());
console.log("Pick 3:", pickFrom(3, list).toString());
console.log("Pick 3:", pickFrom(3, list).toString());

Solution 4:

You can use a array for storing question that have been asked Check array every time during randomization and then select question

var track=newArray();

while(true)//loop until Unique number
{
    var randomQuiz=Math.floor(Math.random()*quiz.length);

     if(track.indexOf(randomQuiz)==-1)//if you have got your unique random number
     {
        track.push(random);
        break;
     }
}

Edit: as Stephen P pointed out it may lead to performance issue, removing element from array is more logical thing to do as suggested by Brent Waggoner.

Solution 5:

You need to write some logic. Below is one sample example.

var list = [1, 2, 3, 4, 5];
    var listDone = [];        
    var inProcess = true;
    while(inProcess){
        var randomQuiz = Math.floor(Math.random() * list.length);

        var isDone = false;
        for (var j = 0; j < listDone.length; j++) {
            if (listDone[j] === randomQuiz)
                isDone = true;
        }
        if (!isDone) {
            console.log(list[randomQuiz]);//Display if not Done.
            listDone.push(randomQuiz);
        }

        if (list.length == listDone.length)
            inProcess = false;

    }

Post a Comment for "Javascript Random On Array Without Repeat"