Does any one know how to ready this Math random and what are they trying to accomplish?

Does any one know how to ready this Math random and what are they trying to accomplish?


var rand = Math.floor(Math.random() * (index - 1)) + 1;
this code but I still do want to learn
how all the code’s bellow work… .

function populateRoundAnswers(gameQuestionIndexes, correctAnswerIndex, correctAnswerTargetLocation) {
    // Get the answers for a given question, and place the correct answer at the spot marked by the
    // correctAnswerTargetLocation variable. Note that you can have as many answers as you want but
    // only ANSWER_COUNT will be selected.
    var answers = [],
        answersCopy = questions[gameQuestionIndexes[correctAnswerIndex]][Object.keys(questions[gameQuestionIndexes[correctAnswerIndex]])[0]],
        temp, i;

    var index = answersCopy.length;

    if (index < ANSWER_COUNT){
        throw "Not enough answers for question.";

    // Shuffle the answers, excluding the first element.
    for (var j = 1; j < answersCopy.length; j++){
        var rand = Math.floor(Math.random() * (index - 1)) + 1;
        index -= 1;
****************//also the code here 
        var temp = answersCopy[index];
        answersCopy[index] = answersCopy[rand];
        answersCopy[rand] = temp;

    // Swap the correct answer into the target location
    for (i = 0; i < ANSWER_COUNT; i++) {
        answers[i] = answersCopy[i];
    temp = answers[0];
    answers[0] = answers[correctAnswerTargetLocation];
    answers[correctAnswerTargetLocation] = temp;
    return answers;

Also much love and Be Blessed :sunny:!!

~K.C Diez


Break it down into pieces.

var rand = Math.floor(Math.random() * (index - 1)) + 1;

is the same as

var rand = Math.random(); // a random number between 0 and 1 -- say 0.75
rand = rand * (index - 1); // multiply the random number by one less than the index -- say index is 3 and rand is now 1.5 
rand = Math.floor(rand); // round down to a whole number -- this would be 1 in our example
rand = rand + 1; // add 1 -- in our example rand is now 2


random is so inconvenient in javascript… thankfully they don’t demand us to seed it manually

also you can use your browser’s js console (ctrl + shift + i) to run the snippet and its parts (assuming you define the variables or substitute them for actual numbers) to see the immediate result


Ariell LOVE your photo Icon, also thank you so much for the time and attention on my post and god that’s a really good advice why not break down the problem(ofcourse not all problem’s are going to be break down, any way thank you for the support and love.)

Be blessed.

~KC. Diez

P.S also in the code I know you suppose to read for loop like this .

var j = 1; then j < answerCopy.length;
then var rand = etc…
so my question is should I read the j++ first or the indext -=1? also do the loop will keep doing the var random = math.floor(math.ran … etc? w8 should I repeat all the codes inside the for loop?

 for (var j = 0; j < GAME_LENGTH; j++){
        var rand = Math.floor(Math.random() * index);
        index -= 1;

        var temp = indexList[index];
        indexList[index] = indexList[rand];
        indexList[rand] = temp;

    return gameQuestions;

the GAME_LENGTH by the way is = to 5 and the var index = question.length which has 31 question,