Can JS create truly random words?

I know that JS can select a random value from an array of predefined content. But is it possible for JS to create a truly random word? Without an array that tells it what to choose from?

And if so, how can I do that?

I’m using Javascript to code a RNG algorithm for a videogame art project in which I let JS decide on the contents of the video in order to make it truly random (things such as shot length, shot contents, F-stop values etc. I wanna use the random words as inspiration for custom maps I’m gonna make for it.

yes, if you want random sets of letters. If you want a random meaningful word it needs to come from a set of predefined words

Well, you have two issues here.

  1. Can computers create randomness?
  2. Can computers create words without a list?

First, computers can’t normally do “random”. It’s a deterministic system. But they can use deterministic formulas to create something that very closely approximates randomness. When people say random with regards to a computer, that is usually what they mean.

As to words, you mean English words, like “apple” or “bunny”? A computer could create a random string of letters at a random length, but how will it know if it is a real word? Is there a list somewhere where it can look it up? An API? English has 158K five letter words. There are 11.88M letter combinations. So, it will take an average of 75 tries to hit a word. And that’s for five letters, it’s probably worse for others. And you still need an API to hit to check.

I would think that these are the available approaches:

  1. Create a local list.
  2. Create a local data object, like a tree structure to house the words, each node with 16 possible branches. That could be a more efficient way to store. Then you traverse the tree, hitting random branches until you hit a leaf.
  3. Find and API that will return random words.
  4. Generate random groups of letter and check them over an API. This could get ugly.
1 Like

I understand. Random sets of letters would be fine! Whether JS would return ‘Sok36’ by accident or ‘Wrdkr55’, that would be all fine! Would you be willing to show me how to do that?

Oof. That would be too difficult for me at this time. I haven’t gone over API’s yet. Like if JS were to return pseudowords or random sets of letters that would also be fine! The randomized results kind of serve as a subject or theme or inspiration for the maps.

You could use String.fromCharCode() and random numbers: 48-57 it’s numbers, 65-90 uppercase letters, and 97-122 lowercase letters

(wordMax is approximating dial for word size)

function makeWord(wordMax) {
let text = ''
const possible='bcdfghjklmnpqrstvwxyz'
const possibleVowels='aeiou'

  for(let i=0; i<wordMax; i=i+3){
    text += possible[Math.floor(Math.random()*possible.length)]
    text += possibleVowels[Math.floor(Math.random()*possibleVowels.length)]
    text += possible[Math.floor(Math.random()*possible.length)]
  }
return text
}
console.log(
  makeWord(8)
)   

(you can easily manipulate something like this to create a map of chosen syllables picked at random for example. i thought it might help if you’re doing something artsy. )

A random word is not just a bunch of random letters. Do you need an actual word or what?


I’m sure there are simple random word APIs that you can use. It really doesn’t take long to learn to use fetch with an API. The main thing you have to be aware of is the shape of the data the API returns.

Example:

const randomWord = document.getElementById("word");

const getWord = async () => {
  const res = await fetch("https://random-words-api.vercel.app/word");
  if (res.ok) {
    const json = await res.json();
    randomWord.innerHTML = json[0].word;
  }
};

getWord();

If you just want random letters, that’s easy enough:

const getRandomIntegerInRange = (min, max) => Math.floor(Math.random() * (max + 1 - min)) + min

const generateLowercaseLetter = () => String.fromCharCode(getRandomIntegerInRange(97, 122))

const generateWord = (minLength, maxLength = 0) => {
  const wordLength = minLength >= maxLength ? minLength : getRandomIntegerInRange(minLength, maxLength)
  return new Array(wordLength).fill(null).map(() => generateLowercaseLetter()).join('')
}

This topic was automatically closed 182 days after the last reply. New replies are no longer allowed.