0

I am trying to take a set of words for example...

Apple
Banana
The
Orange
House
Boat
Lake
Car
And

I want to be able to output 50 of these words at random to a div using javascript. Is this possible and if so should I be looking at putting them into an array or something similar?

1
  • Of course this is possible. Start with an array ... and if you have any specific problem, you can come back to us. Commented Sep 7, 2012 at 10:47

4 Answers 4

2

Yes, that's possible, using the Math.random function to generate the text and innerHTML to fill a div.

The array make it easier to get the words.

var tokens = ['Apple', 'Banana', 'The' ...];
var text = '';
for (var i=0; i<50; i++) {
    text += tokens[Math.floor(Math.random()*tokens.length)];
}
document.getElementById('myDiv').innerHTML = text;
Sign up to request clarification or add additional context in comments.

Comments

2

Yes, use an array. To get a random word from such an array, use the following:

function getRandomWord() {
   var words = ['Apple','Banana','The','Orange','House','Boat','Lake','Car','And'];
   return words[Math.floor(Math.random() * words.length)];
}

Then invoke that function to whatever extent you like.

var paragraph = [];
for(var i = 0; i < 50; i++)
    paragraph.push(getRandomWord());

$('#my-container').text( paragraph.join(' ') );

1 Comment

Did he say, that he is using jQuery? :)
1

That's possible and here is a quick and dirty example of it. (Sorry I've got no more time atm)

var words = ['apple', 'beer', 'cake', 'potato', 'orange'],
    div = document.getElementById('foo');

for(i = 0; i < 50; i++) {
    div.innerHTML += ' ' + words[Math.floor(Math.random() * words.length)];
}

http://jsfiddle.net/LavY5/

Comments

1

Yes, put all your words in an Array. Then apply the Fisher-Yates shuffle algorithm, to shuffle/randomize your words array. Then, take a slice of the array of your specified size, and print those words to the HTML:

var words = [ "Apple", "Banana", "The", "Orange", "House", "Boat", "Lake", "Car", "And" ];

function fisherYates (arr) {
  var n = arr.length;
  while (n) {
    var j = Math.floor(Math.random() * n--);
    var temp = arr[n];
    arr[n] = arr[j];
    arr[j] = temp;
  }
  return arr;
}

fisherYates(words); // shuffle
var randomParagraph = words.slice(0, 5).join(" "); // take 5 random words
document.write(randomParagraph);

DEMO. ​

4 Comments

I think this isn't right, the last remaining position is already shuffled, and to exchange it with itself is useless. Take a look on the last table of section Modern method in the Wikipedia article
I also think the shuffling is a bit distracting for this case.
The n-- I understand, but seems a bit dangerous, I'd give it another line. Howsoever, if you can convince me about the need for F&Y her, I'll vote up ;)
I see the problem of "randomizing an array" as a different wording for "shuffling an array", for which Fisher-Yates was designed for. Thus, if you have an array of, say, 1000 words, and you want to take N random ones, you have two options: 1) Do N iterations and take a random word each time, and having to worry about the possibility of picking the same word every time; 2) Randomizing/Shuffling the array, and then pick the first N words (this avoids duplication of elements as well).

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.