0

I'm kind of new to JavaSCript and I want to ask wow can I make a random anagram generator, without a list of all possible anagrams (because that's not optimized)

Example:

input: "abc"

output: get the input, split it (["a", "b", "c"]), randomize the split and join them (["cba"])

This is for a command for my bot in Discord.

I've searched about this, but I just found generators for all anagrams possible in the same string.

Thank you for reading this, have a nice day

Zsolt Meszaros
  • 21,961
  • 19
  • 54
  • 57

1 Answers1

2

Well, you've explained it yourself. You just had to search for the commands online...

const input = 'abc';
const output = input
  .split('') // make it into strings
  .sort(() => 0.5 - Math.random()) // shuffle
  .join(''); // convert to a string

Not sure if you need the result to be an array of a single item, or just a string :|

Also, keep in mind the output might be the same as the input! If you don't want this - call the function as many times as needed in order to get a different output.


Edit: as @PA mentioned, using the above-mentioned way to sort arrays is not a great idea. Even though it might currently work, it can lead to bad results. The reason is that the sort algorithm might (not sure) need static values for each item. Theoretically, that might be a problem. Therefore something like this can be used:

const input = 'abc';
const output = input
  .split('') // make it into strings
  .map((v) => [Math.random(), v]) // convert the array of characters into tuples (pairs)
  // of a random element and the real value
  .sort((a, b) => a[0] - b[0]) // sort, based on the random values
  .map(([rand, value]) => value) // return to list of characters
  .join(''); // convert to a single string

This way you set the random value once, then do the shuffling, then remove the random values.

Andrey Popov
  • 7,362
  • 4
  • 38
  • 58
  • Never ever shuffle by sorting with a random comparator ! because it breaks the sort internal logic and can lead non random solutions and event to infinite loops. Use a well known algorithm instead. See https://stackoverflow.com/questions/375351/most-efficient-way-to-randomly-sort-shuffle-a-list-of-integers-in-c-sharp – PA. Oct 28 '22 at 09:23
  • Not sure how that helps in any way with this particular question :) But out of curiosity - how can a JS sort using random can lead to infinite loops? – Andrey Popov Oct 28 '22 at 09:26
  • well, the internal workings of the sort algorithm is a black box. A random comparison function is not deterministic. The algorithm assumes that if a>b and b>c then a>c but a random comparator does not honor this assumption, so the algorithm might get lost, confused and it might lead in certain implementations to loop forever or take inusual long times. See https://rufflewind.com/2016-09-16/random-comparator-sort – PA. Oct 28 '22 at 12:38
  • "From an engineering perspective, sorting functions are designed to sort things. Abusing it to shuffle an array is kind of just asking for trouble, because you never know if a change in the sorting algorithm down the road might break the “shuffling” algorithm. Worse, the breakage could be subtle, creating biases in the output that might be hard to detect unless the tests rigorously verify the statistics. Moreover, some sorting algorithms can run unusually slow with a random comparator, or take a very long time if the randomness was not in their favor." – PA. Oct 28 '22 at 12:39
  • I agree with your point, even thought it's highly theoretical, and having the context of this question, it doesn't matter :D But I'll update my post with another solution ;) – Andrey Popov Oct 28 '22 at 12:53
  • I've done a little experiment. And each comparison can be evaluated differently because the sort callback is dynamic (obviously :D). Because of that, it's relatively simple to have at least many iterations. And in theory, those iterations might never end. I'll need to do another test to figure out what kind of sorting they use :) Entirely not related to the topic, but still an excellent brain game, thanks! – Andrey Popov Oct 28 '22 at 19:07
  • afaik javascript sort uses QuickSort algorithm – PA. Oct 30 '22 at 07:59