Yeah, apparently - I think it boils down to a question of sort() having a strong bias for keeping elements in their initial positions (there's a bit of a discussion about it on one of the answers here: stackoverflow.com/questions/245095...).
Thanks for the tip regarding .getRandomValues(). How would that work with strings or objects?
Haha! I like this from that site: But due to the utter randomness of the comparison the black box goes mad, and how exactly it goes mad depends on the concrete implementation that differs between engines.
So yes, I think it's down to the JavaScript runtime itself where the problem lies.
Seasoned C++ developer with Physics, Maths, Medical Imaging background. Experience with PHP, Python, SQL (MariaDB, PostgreSQL). Into learning Vue/Quasar/supabase/TypeScript and sustainable tech.
Education
University of Tübingen and Potsdam, Max-Planck-Institute for Gravitational Physics
Well, the generic Array.sort() algorithm puts several requirements on the comparison function, see developer.mozilla.org/en-US/docs/W... which in turn it may use to implement the sorting algorithm. In the "Custom sort" approach above, in particular the requirement of the comparison to be "stable" is violated. So, you are breaking the Array.sort() contract and may receive any result. In the worst case, the algorithm might not even terminate.
This exports a randomly sorted array of questions.
varmap=newMap()map.set(uuid4(),"What inspires you?")map.set(uuid4(),"Who is someone in your life you consider a hero and why?")map.set(uuid4(),"What is your favorite snack?")map.set(uuid4(),"What words of wisdom do vou live by?")map.set(uuid4(),"What is your favorite holiday tradition?")map.set(uuid4(),'Please complete this sentence."Happiness is..."')exportconstQUESTIONS=[...map].sort(([a],[b])=>String(a).localeCompare(b)).map((a)=>a[1])
Tip: You can swap values using array destructuring:
I wouldn't recommend this kind of thing in a production codebase. If you have experience with lists in Python this might seem familiar though.
Is the first method really flawed?
Also, rather than Math.random(), I can always use crypto.getRandomValues()
developer.mozilla.org/en-US/docs/W...
Yeah, apparently - I think it boils down to a question of
sort()
having a strong bias for keeping elements in their initial positions (there's a bit of a discussion about it on one of the answers here: stackoverflow.com/questions/245095...).Thanks for the tip regarding
.getRandomValues()
. How would that work with strings or objects?I read the solution part of javascript.info/array-methods#shuf..., but not really understand the logic, or is it the fault of JS Engine?
For
.getRandomValues()
, there are two more methods of shuffling, but of course, not performant, even if your first method is not faulty.Haha! I like this from that site:
But due to the utter randomness of the comparison the black box goes mad, and how exactly it goes mad depends on the concrete implementation that differs between engines.
So yes, I think it's down to the JavaScript runtime itself where the problem lies.
Well, the generic
Array.sort()
algorithm puts several requirements on the comparison function, see developer.mozilla.org/en-US/docs/W... which in turn it may use to implement the sorting algorithm. In the "Custom sort" approach above, in particular the requirement of the comparison to be "stable" is violated.So, you are breaking the
Array.sort()
contract and may receive any result. In the worst case, the algorithm might not even terminate.Fisher-Yates is orders of magnitude faster, and gives a less biased result. There is an excellent visual write-up of the algorithm here.
How about creating a new ordered Hash key value data structure, and use randomly generated UUID as a key?
This exports a randomly sorted array of questions.
Why does JS have no standart method Array.prototype.shuffle()?