Pick random but unique array in JavaScript

get random elements from array javascript
javascript pick multiple random from array
javascript pick random from array
shuffle(array javascript)
javascript random array element no repeat
javascript random entry in array
pick random array element
choose random index javascript

I am working on a project in which I need to create a simple html page questionnaire for work. Its been more that 8 years I have not done any programming. Now suddenly be given a project. Stack Overflow helped me in getting through most of the project. But now I have hit a wall.

I need to pick a random but NO repeated question from array of questions. I am using JavaScript to display the questions and using this code example as basis for project.

<!DOCTYPE html>
<html lang="en">
<meta charset="utf-8"/>

<script type="text/javascript">

var pos = 0, test, test_status, question, choice, choices, chA, chB, chC, correct = 0, randomq=0;

var questions = [
  ["What is 36 + 42", "64", "78", "76", "B"],
  ["What is 7 x 4?", "21", "27", "28", "C"],
  ["What is 16 / 4?", "4", "6", "3", "A"],
  ["What is 8 x 12?", "88", "112", "96", "C"]

function get(x){
  return document.getElementById(x);


function renderQuestion(){
  test = get("test");

  if(pos >= questions.length){
    test.innerHTML = "<h2>You got "+correct+" of "+questions.length+" questions correct</h2>";
    get("test_status").innerHTML = "Test completed";

    // resets the variable to allow users to restart the test
    pos = 0;
    correct = 0;

    // stops rest of renderQuestion function running when test is completed
    return false;

// shuffle questions

  random1 = Math.floor(Math.random() * (questions.length)) ;
   get("test_status").innerHTML = "Question "+(pos+1)+" of "+questions.length;

  question = questions[random1][0];
  chA = questions[random1][1];
  chB = questions[random1][2];
  chC = questions[random1][3];
  test.innerHTML = "<h3>"+question+"</h3>";
  // the += appends to the data we started on the line above
  test.innerHTML += "<input type='radio' name='choices' value='A'> "+chA+"<br>";
  test.innerHTML += "<input type='radio' name='choices' value='B'> "+chB+"<br>";
  test.innerHTML += "<input type='radio' name='choices' value='C'> "+chC+"<br><br>";
  test.innerHTML += "<button onclick='checkAnswer()'>Submit Answer</button>";
function checkAnswer(){
  // use getElementsByName because we have an array which it will loop through
  choices = document.getElementsByName("choices");
  for(var i=0; i<choices.length; i++){
      choice = choices[i].value;
  // checks if answer matches the correct choice
  if(choice == questions[random1][4]){
    //each time there is a correct answer this value increases
  // changes position of which character user is on
 // then the renderQuestion function runs again to go to next question
window.addEventListener("load", renderQuestion, false);
<h2 id="test_status"></h2>
<div id="test"></div>

I am looking for a way that there is no repeated questions.

Please advice...

Thanks in advance.

A dirty hack would be to create a boolean array whose size would equal to the number of questions. Initialize all values to false.

Then select a random question, and set the corresponding boolean value to true, if it's false. If it's true, that means it has been used before, so skip the question and move one until all values are true.

Unique random item from array, In JavaScript, I would like to randomly remove an item from an array and display this within the HTML on a button click. Then on the next click of  Of course, we can use this function to pick any random value from an array, not only colors. We could stop here and call it a day, but let's take a look at the randomColor and see what it does, bit by bit. Math.floor(Math.random() * colors.length) generates a random index for the colors array;

Why not just splice out the used questions?

questionArr.splice(index, 1):

Or if you want to keep the used questions save them in a usedQuestions array and do questionArr = [...usedQuestions]; usedQuestions = []; When the questionArr is empty

How to get multiple, random, unique elements from a JavaScript array, As a note to self, this JavaScript code gets multiple, random, unique elements from a JavaScript array and displays three of those elements in  The task is to select the random element from the array using JavaScript. Approach 1: Use Math.random() function to get the random number between(0-1, 1 exclusive). Multiply it by the array length to get the numbers between(0-arrayLength).

Thank you all for help.

i have found the solution with : fisher-yate shuffle


Select Random Item from an Array, This is super succicnt, and here I was doing this all the time like a total schmo… … JS Result EDIT ON function randomNum(minVal, maxVal) { do  Getting multiple random unique elements from an array: In JavaScript, it is possible to get multiple random elements that are unique. Submitted by Shivang Yadav, on July 30, 2019 The JavaScript is a very versatile language and it has a function almost everything that you want.

Selecting 6 random elements from an array, I want to create a function which will select 6 random elements from an array of 16 elements and return another array of the elements randomly  As a note to self, this JavaScript code gets multiple, random, unique elements from a JavaScript array and displays three of those elements in the second div: <html> <body> <div id="one"> <p>one</p> </div> <div id="two"> <p>two</p> </div> <script> function shuffle(a) { for (let i = a.length - 1; i > 0; i--) { const j = Math.floor(Math.random() * (i + 1)); [a[i], a[j]] = [a[j], a[i]]; } return a; } var arr = [ "<span class=\"booklink\"><a href=\"/one\">one</a></span>", "<span

How to select a random element from array in JavaScript , A Computer Science portal for geeks. It contains well written, well thought and well explained computer science and programming articles, quizzes and  See the Pen JavaScript - Get a random item from an array - array-ex- 35 by w3resource (@w3resource) on CodePen. Improve this sample solution and post your code through Disqus Previous: Write a JavaScript function to get nth largest element from an unsorted array.

How to get all unique values (remove duplicates) in a JavaScript , It contains well written, well thought and well explained computer science and If the current value does not exist in the output array with unique values, then add the How to create an array with random values with the help of JavaScript ? The task is to pick a random color from an array using CSS. There is no way to do this using CSS only. CSS doesn’t support logical statements because CSS is purely deterministic (there is nothing like array etc in CSS). We can use client-side JavaScript to select a random color from the array.

  • shuffle the array once, then .splice() out the questions until the array is empty.