How to random many link in javascript

hi thanks before your help i have question and please help

i have code

var r_text = new Array();
r_text[0] = {
  msg: "aaaaaaaaaaaaaaaaaaaaaaa",
  link: "http://www.aaaaaaaaaaaaaaaaaaaaaaa.com"
};
r_text[1] = {
  msg: "bbbbbbbbbbbb",
  link: "http://www.bbbbbbbbbbbb.com"
};
r_text[2] = {
  msg: "ccccccccccccccccccccc",
  link: "http://www.ccccccccccccccccccccc.com"
};
r_text[3] = {
  msg: "gggggggggggggggggggggggggg",
  link: "http://www.gggggggggggggggggggggggggg.com"
};
r_text[4] = {
  msg: "ttttttttttttttttttttttttttttttttttttttttttt",
  link: "http://www.ttttttttttttttttttttttttttttttttttttttttttt.com"
};

var i = Math.floor(r_text.length * Math.random());

document.write("<br /><br /><br /><br /><br /><br /><br /><center><FONT SIZE=72><FONT COLOR='#FFFFFF'><a href='" + r_text[i].link + "'>"+
  r_text[i].msg + "</a></FONT></center><br />");

this a little result but how when i have thousand link ?

Answer

The most efficient way is to build a string then write to the DOM (rather than write to the dom each iteration). You can randomize your array first, then map the random indexes to HTML and finally append the document with it.

/* For testing - this will simulate 10000 links */
let r_text=[], p = "At vero eos et accusamus et iusto odio dignissimos ducimus qui blanditiis praesentium voluptatum deleniti atque corrupti quos dolores et quas molestias excepturi sint occaecati cupiditate non provident, similique sunt in culpa qui officia deserunt mollitia animi, id est laborum et dolorum fuga. Et harum quidem rerum facilis est et expedita distinctio. Nam libero tempore, cum soluta nobis est eligendi optio cumque nihil impedit quo minus id quod maxime placeat facere possimus, omnis voluptas assumenda est, omnis dolor repellendus. Temporibus autem quibusdam et aut officiis debitis aut rerum necessitatibus saepe eveniet ut et voluptates repudiandae sint et molestiae non recusandae. Itaque earum rerum hic tenetur a sapiente delectus, ut aut reiciendis voluptatibus maiores alias consequatur aut perferendis doloribus asperiores repellat".split(" ");
for(let x=0;x<10000;x++) {
let n=6, m=[];
while(n-->0) m.push(p[Math.max(0, Math.floor(Math.random() * p.length-6))])
r_text.push({msg: m.join(" "), link:'https://google.com'})
}
 

// ye-olde Durstenfeld shuffle
const shuffle = (array) => {
  for (let i = array.length - 1; i > 0; i--) {
    const j = Math.floor(Math.random() * (i + 1));
    [array[i], array[j]] = [array[j], array[i]];
  }
  return array;
}
// set up your indexes
let all = [], x
for (x = 0; x < r_text.length; x++) all.push(x);;
all = shuffle(all); // randomize

// map over the html
let html = all.map(i => `<div class='item'><a href='${r_text[i].link}'>${r_text[i].msg}</a></div>`).join('');

document.querySelector('#container').innerHTML = html;
.item {
  margin: 5px;
  padding: 15px;
  background: #f0f0f0;
  border-radius:8px;
}
<div id='container'></div>