Shuffle An Array in JavaScript
Updated: March, 2026
Random History
bittybitty provides a . function for arrays. It uses the Fisher–Yates/Knuth shuffleshuffle algorithm. Knuth shuffle sounds like a dance from the mid 20th century, but it's a way to jumble an array using a random number generator.
I've used JavaScript's Math. to generate those numbers for years. For bitty, I put in an upgrade.
crypto, not crypto
JavaScript has had a better way to generate random numbers since 2015history. It's called crypto. and I never see it mentionedstuck.
This version of crypto is not a currency. It's a way to generate "cryptographically strong random values".
Random Changes
Using Math
The old Math.random()js version of the function looks like this:
JavaScript
Math.random() Output
Using crypto
Here's the update with crypto. in place:
JavaScript
crypto.getRandomValues() Output
The Little Differences
Frankly, I don't know if there will be a perceptual difference between it and Math.. It's hard to argue against better randomness when randomness is what you're looking for, though.
-a
Endnotes
These functions modify the existing array directly.
There is an impact on performance switching to crypto.. It's ~25x slower on my machine. For example, here's the times it takes to shuffle an array with 1,000,000 items in it:
Math.random() crypto.getRandomValues() 22ms 620ms 20ms 668ms 20ms 627ms 19ms 620ms 21ms 613ms 24ms 610ms 20ms 623ms 22ms 622ms 21ms 621ms 23ms 618ms Avg: 21.2ms Avg: 624.2ms
But, that's a million items. I've never used an array that big in JavaScript. If you cut the array about to 1,000 items the times drop to an avg of 1.4ms.
Sure, it's slower. For my use cases, the difference is imperceptible. I'm unconcerned with the impact.
Footnotes
Designed to make it easier for folks to make stuff for the web.
Jumble with confidence.
The MDN page lists crypto.getRandomValues() as being widely available since 2015. I expect it's been around longer than that in some engines.
crypto.getRandomValues() is very different approach to getting a random number. You create an array with a single slot, fill that slot via a . call then divide that number and pull the remainder. It's a couple extra lines, but I never see it get used. I expect a big part of that is that the top Stack Overflow answers use Math..
It's "go with what you know" syndrome.
A problem with the potential to get worse the more folks use AI. With so many examples using Math., the AI stats modules seem unlikely to provide the update. Generated code and examples will use the old version as a result. That'll further push the new version down the model.
Rinse, repeat.