1

I'd like to create dummy text with 1000 words using javascript. My idea is that use an array like this

var words =["The sky", "above", "the port","was", "the color of television", "tuned", "to", "a dead channel", ".", "All", "this happened", "more or less","." ,"I", "had", "the story", "bit by bit", "from various people", "and", "as generally", "happens", "in such cases", "each time", "it", "was", "a different story","." ,"It", "was", "a pleasure", "to", "burn"];

then use javascript output 1000 words randomly. How would I do that using javascript? any other ways to do it?

Tamn
  • 147
  • 2
  • 9
  • Possible duplicate of [Getting random value from an array](http://stackoverflow.com/questions/4550505/getting-random-value-from-an-array). – Phil Dec 06 '15 at 22:37
  • 1
    `for (var i=1000; i--;) document.body.innerHTML += words[Math.floor(Math.random()*words.length)];` – adeneo Dec 06 '15 at 22:38

6 Answers6

7

look at this

var words =["The sky", "above", "the port","was", "the color of television", "tuned", "to", "a dead channel", ".", "All", "this happened", "more or less","." ,"I", "had", "the story", "bit by bit", "from various people", "and", "as generally", "happens", "in such cases", "each time", "it", "was", "a different story","." ,"It", "was", "a pleasure", "to", "burn"];
var text = [];
var x = 1000;
while(--x) text.push(words[Math.floor(Math.random() * words.length)]);
document.write(text.join(" "))

Reference: SO | Getting random value from an array

Community
  • 1
  • 1
Santiago Hernández
  • 4,983
  • 2
  • 24
  • 33
  • I think you should add spaces between the words. – CoderPi Dec 06 '15 at 22:41
  • 1
    Nice. So your answer will provide a "sentence" with multiple occurences of the words. And my answer will use every word exactly once. Answers completed – CoderPi Dec 06 '15 at 22:43
  • I followed you but it made a mistake so it doesn't work well, I couldn't firgure it out. I have another suggestion remove the loop but I dont know how to do it. http://codepen.io/tamnguyen/pen/vLBQNj – Tamn Dec 07 '15 at 00:37
2
    var m = words.length,
    t, i,result=[];
while (m && result.length < 100) {
    i = Math.floor(Math.random() * m--);
    t = arr[m];
    arr[m] = arr[i];
    arr[i] = t;
    result.push(arr[m]);
}
Jerry Chen
  • 695
  • 7
  • 19
2

As I personally love Lorem Ipsum, what about this?

var r = new XMLHttpRequest();
r.onload = function() {
    if (r.readyState != 4 || r.status != 200) return;   // bad query or something...
    document.getElementById('LIpsum').textContent = r.responseText;
};
r.open('GET', 'https://baconipsum.com/api/?type=meat-and-filler&paras=18&format=text');
r.send();

Docs on Bacon Ipsum (the only Ipsum I know to allow CORS requests): http://baconipsum.com/json-api/

hKaspy
  • 106
  • 5
1

This will take all your words in a random order:

function shuffle(array) {
  var currentIndex = array.length, temporaryValue, randomIndex ;

  // While there remain elements to shuffle...
  while (0 !== currentIndex) {

    // Pick a remaining element...
    randomIndex = Math.floor(Math.random() * currentIndex);
    currentIndex -= 1;

    // And swap it with the current element.
    temporaryValue = array[currentIndex];
    array[currentIndex] = array[randomIndex];
    array[randomIndex] = temporaryValue;
  }

  return array;
}

var words =["The sky", "above", "the port","was", "the color of television", "tuned", "to", "a dead channel", ".", "All", "this happened", "more or less","." ,"I", "had", "the story", "bit by bit", "from various people", "and", "as generally", "happens", "in such cases", "each time", "it", "was", "a different story","." ,"It", "was", "a pleasure", "to", "burn"];

shuffle(words);
var sentence = words.join(" ")

// Demo
console.log(words);
console.log(sentence);
document.write(sentence)

Using shuffle() from How to randomize (shuffle) a JavaScript array?

Community
  • 1
  • 1
CoderPi
  • 12,423
  • 4
  • 31
  • 59
1

var words = ["The sky", "above", "the port", "was", "the color of television", "tuned", "to", "a dead channel", ".", "All", "this happened", "more or less", ".", "I", "had", "the story", "bit by bit", "from various people", "and", "as generally", "happens", "in such cases", "each time", "it", "was", "a different story", ".", "It", "was", "a pleasure", "to", "burn"];

function randomSentence() {
  var n = 1000;
  var sentence = "";
  while (n--) {
    sentence += words[Math.floor(Math.random() * words.length)] + " ";
  }
  return sentence;
}
document.writeln(randomSentence())
CoderPi
  • 12,423
  • 4
  • 31
  • 59
1

I'd separate the punctuation and capitalization, and count the words, not the items used in the array. Here's a jsfiddle:

https://jsfiddle.net/eexLwt4L/1/

Here's the code:

var words =["the sky", "above", "the port","was", "the color of television", "tuned", "to", "a dead channel", "all", "this happened", "more or less" ,"I", "had", "the story", "bit by bit", "from various people", "and", "as generally", "happens", "in such cases", "each time", "it", "was", "a different story", "it", "was", "a pleasure", "to", "burn"],
    punctuation = [".", ","],
    text = "",
    phrase,
    punc,
    count = 0,
    nextCapital = true;
while(count<1000) {
  phrase = words[Math.floor(Math.random() * words.length)]
  text += nextCapital ? phrase[0].toUpperCase() + phrase.slice(1): phrase;
  nextCapital = false;
  if ( Math.random() > .8 ) {
    punc = punctuation[Math.floor(Math.random() * punctuation.length)];
    if ( punc === "." ) nextCapital = true;
    text += punc;
  }
  text += " ";
  count = text.match(/\S+/g).length;
}
document.write(text);