Skip to content Skip to sidebar Skip to footer

Generate Random List Of Words With Js

I'm trying to build a random JS word list generator but the code I have here just generates a single word. Actually I want it to generate a list of 30 words from a previously given

Solution 1:

If you want to grab N items randomly from an array, a classical way of doing it is to :

  1. randomly shuffle your array,
  2. pick up the N first items of the suffled array

Example code :

functionsamples(items, number){
    items.sort(function() {return0.5 - Math.random()});
    return items.slice(0, number);
}

Notice that the shuffle algorithm is probably not the best one, it is provided as an example, as mentioned by @Phylogenesis.

If you prefer to avoid reinvent wheels, you can also use undercore.js for example, that provides a sample method

Post a Comment for "Generate Random List Of Words With Js"