Problems with shuffling array of HTML links

pulseira

I'm running a site where the user's only way of navigating the subdirectories is via random pages (akin to Wikipedia's Random Page feature). I already implemented a code to call up random pages and it works fine, but I want to minimize the chance of the same page being called up again after the onclick execution.

I stumbled over the Fisher-Yates-Knuth shuffle while researching a better method and tried to implement it into my script:

function Next() {
   var links = [];
   links[0] = "/arch/g1";
   links[1] = "/arch/g2";
   links[2] = "/arch/g3";
   links[3] = "/arch/g4";

   var m = links.length, t, i;
   while (m) {
      i = Math.floor(Math.random() * m--);
      t = links[m];
      links[m] = links[i];
      links[i] = t;
   }
   window.location = links[m]
}

The script basically works, but it still results in calling up the same page twice or even three times in a row. I'm still pretty new at JS, so it'd be great if someone could point me in the right direction.

freethinker

Eeach call on Next() function is shuffle array of links and get one index from array. You have 4 different links, on calling this function 3 times you have good chance to get same page.

Try this:

var links = ["/arch/g1","/arch/g2","/arch/g3","/arch/g4"];

function shuffle(links) {
    var m = links.length, t, i;
    while (m) {
    i = Math.floor(Math.random() * m--);
    t = links[m];
    links[m] = links[i];
    links[i] = t;
    }
}

function Next(){
    if(links.length === 0){
        links = ["/arch/g1","/arch/g2","/arch/g3","/arch/g4"];
        shuffle(links);
    }

    var nextUrl = links[links.length - 1];
    links.splice(links.length - 1, 1);
    window.location = nextUrl;
}

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Problems with shuffling array of HTML links

From Dev

Problems with shuffling characters in a string

From Dev

Problems with shuffling arrays in numpy?

From Dev

Shuffling an Array with another array

From Dev

history.back() and html links problems

From Dev

Random shuffling of String array

From Dev

Shuffling or Randomizing an Array in C

From Dev

Array is not shuffling in android

From Dev

Shuffling a multidimensional dynamic array

From Dev

Shuffling an array of (struct) in c

From Dev

Problems rendering large html files (links are wrong, images not displaying)

From Dev

Error while shuffling an Array in Java

From Dev

preserving the indecies before shuffling an array

From Dev

Shuffling the first level of the array in PHP

From Dev

Shuffling array.Output not correct

From Dev

Random Shuffling an array of integers in Java

From Dev

Shuffling an array: Why does this work?

From Dev

Populate HTML Table with Array and Create Links

From Dev

Problems with pushing object into array CSS | JS | HTML

From Dev

problems with InAppBrowser on dynamic links

From Dev

Shuffling character array in Java producing NullPointerException

From Dev

Shuffling an array of integers for making secure key pad

From Dev

Shuffling an array of strings in vb.net

From Dev

Shuffling colors in an array to 5 divs jQuery

From Dev

Shuffling multi-dimensional numpy array in python

From Dev

Shuffling an array of integers for making secure key pad

From Dev

Shuffling 2D array of cards

From Dev

shuffling position of three images in html/javascript

From Dev

How to output list of HTML links from an array using Pug?