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
  • 157
  • 3
  • 10
  • Possible duplicate of [Getting random value from an array](http://stackoverflow.com/questions/4550505/getting-random-value-from-an-array). <- just do that 1000 times, pushing each result to an array and then join it on " " or simply append to a string on each iteration – 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

7 Answers7

8

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
  • 5,438
  • 2
  • 26
  • 34
  • 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
  • 715
  • 7
  • 21
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
  • 127
  • 6
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,985
  • 4
  • 34
  • 62
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,985
  • 4
  • 34
  • 62
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);
0

Generating random words from words in a sentence or an array of words.

  1. First define your sentence or array of words
  2. define a function that will generate the random text. in this case we label it generate.
  3. define some variables scoped to the function -text = [], and determine whether the function parameter (words) is an array or a string using Array.isArray() or typeof keyword.
  4. if the parameter(words) is of string type, we will convert to an array using words.split(" ");
  5. Use Math.floor(Math.random() * words.length) to generate random words.
  6. we then use the array.push() method to add the random words to our text array;

const arrayWords =["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"];
const stringWords = "The name of the lord needs to be praised. I am blessed and highly favoured, we are one people. i know his name. he is a great God, she did very well";

// define the function to generate the random words.
//the function can accept either and array of words or a string of words

function generate(words,newSentenceLength=100) {
    if(Array.isArray(words)) words = words
    else if(typeof(words) === "string") words = words.split(" ");
    const text = [];
    for (let i = 0; i <= newSentenceLength; i++) {
        text.push(words[Math.floor(Math.random() *  words.length)]);   
    }
    return text.join(" ").replaceAll((/\s?\.+/gmi) ,".");
    //the replaceAll and the regular expression is to remove whitespaces preceding a dot(.) or remove multiple dots(..++).
}

// you can pass in two paramaters. the first paramater being either arrayWords or stringWords and the second parameter being a number indicating the number of words to be generated(default is 100);
const newSentence = generate(arrayWords);
const newSentence1 = generate(arrayWords,10);
console.log(newSentence);
console.log(newSentence1);