frontpage.
newsnewestaskshowjobs

Made with ♥ by @iamnishanth

Open Source @Github

fp.

Open in hackernews

The Fisher-Yates shuffle is backward

https://possiblywrong.wordpress.com/2020/12/10/the-fisher-yates-shuffle-is-backward/
33•possiblywrong•5d ago

Comments

robinhouston•4d ago
That’s funny. I’ve always done it the forwards way. I didn’t even realise that wasn’t the usual way.

I suppose one of the benefits of having a poor memory is that one sometimes improves things in the course of rederiving them from an imperfect recollection.

shiandow•2h ago
Huh I didn't know the backwards version was more common, it seems odd.

You could also call the last version the online version, as it will ensure the partial list is random at any point in time (and can be used for inputs with indeterminate length, or to extend a random list with new elements, sample k elements etc.)

Not too sure if the enumerate is necessary. I usually dislike using it just to have an index to play around with. A similar way of doing the same thing is:

    for x in source:
        a.append(x)
        i = random.randint(0, len(a))
        a[i], a[-1] = a[-1], a[i]

Which makes the intention a bit clearer. You could even avoid the swap entirely but you would need to handle the case where i is at the end of the list separately.
dooglius•1h ago
> sample k elements

Not quite sure what you have in mind here, but you need reservoir sampling for this in order to make the selection uniformly random (which I assume is what's desired)

shiandow•1h ago
You can just use this algorithm but ignore everything after the first k elements. The algorithm still works if you don't store anything beyond the first k elements but just pretend they are there.
lupire•45m ago
enumerate() is just an awkward way to get len(a). In theory, you could somehow be in an environment where you have dynamically resizing arrays (vectors) that don't track their length internally. But in this case it's probably because OP doesn't have a firm grasp what's happening (which is why they wrote the blog post).
amluto•1h ago
I find the backward version slightly more intuitive. Here’s why:

Suppose I want to uniformly randomly shuffle a deck of cards in a single pass. I stick the deck on the table and call it the non-shuffled pile. My goal is to move the deck, one card at a time, into the shuffled pile. First I need to select a card, uniformly at random, to be the bottom card of the new pile, and I move it over. Then I select another card, uniformly at random from the still non-shuffled cards, and put it on top of the bottom shuffled card. I repeat this until I’ve moved all the cards, so that each card in the shuffled pile is a uniform random selection from all of the cards it could have been. And that’s it.

One can think of this as random selection, whereas the “forward” version is like random insertion of a not-random card into a shuffled pile. And for whatever reason I tend to think of the selection version first.

dunham•1h ago
For what it's worth, I think of the forward version as randomly selecting a card and putting it at the bottom of the new pile (0..i in the array).
ogogmad•1h ago
The article didn't mention it, but I think you can derive the FY shuffle using base-factorial.

Separate point: One way of deriving 4 different variants of Fisher-Yates is to take a random bijection f:[n]->[n] (where [n] means {0,1,...,n}) drawn using whichever variant of the FY shuffle, and then potentially pre-composing or post-composing with the bijection reverse:[n]->[n] that sends k to n - k - 1.

fn-mote•1h ago
Very interesting article!

For me, the reason for reaching for the “backwards” version first is that it wasn’t as clear to me that the “forward” version makes a uniform distribution.

Even after reading the article.

I appreciated the comment here about inserting a card at a random location, but that also isn’t quite right, because you swap cards not insert. Nevertheless, that did it for me.

NooneAtAll3•56m ago
one algo "creates" shuffled subarray and grows it - the other "chooses" random cards one at a time

both seem intuitive from individual perspectives

fanf2•51m ago
There are actually four variants:

• loop counts downwards vs upwards

• the processed part of the array is a uniform sample of the whole array, or it is a segment that has been uniformly shuffled

Knuth described only the downwards sampling version, which is probably why it’s the most common.

The variants are compared quite well on wikipedia https://en.wikipedia.org/wiki/Fisher%E2%80%93Yates_shuffle

Python 3.15’s interpreter for Windows x86-64 should hopefully be 15% faster

https://fidget-spinner.github.io/posts/no-longer-sorry.html
57•lumpa•2h ago•13 comments

The entire New Yorker Archive Is Now Fully Digitized

https://www.newyorker.com/news/press-room/the-entire-new-yorker-archive-is-now-fully-digitized
24•thm•5d ago•2 comments

Phoenix: A modern X server written from scratch in Zig

https://git.dec05eba.com/phoenix/about/
524•snvzz•16h ago•277 comments

We invited a man into our home at Christmas and he stayed with us for 45 years

https://www.bbc.co.uk/news/articles/cdxwllqz1l0o
397•rajeshrajappan•4h ago•79 comments

Project Dropstone: A Neuro-Symbolic Runtime for Long-Horizon Engineering [pdf]

https://archive.blankline.org/api/media/file/d3_engine_public_release%20(1)-1.pdf
10•epicprogrammer•12h ago•0 comments

Tell HN: Merry Christmas

1509•basilikum•16h ago•345 comments

Mattermost restricted access to old messages after 10000 limit is reached

https://github.com/mattermost/mattermost/issues/34271
196•xvilka•4h ago•103 comments

The First Photographs of Snowflakes Discover the Groundbreaking Microphotography

https://www.openculture.com/2017/12/the-first-photographs-of-snowflakes.html
43•_____k•6d ago•1 comments

Quantum Error Correction Goes FOOM

https://algassert.com/post/2503
31•EvgeniyZh•5h ago•5 comments

Who Watches the Waymos? I do [video]

https://www.youtube.com/watch?v=oYU2hAbx_Fc
208•notgloating•14h ago•62 comments

Self-referencing Page Tables for the x86-Architecture

https://0l.de/blog/2015/01/bachelor-thesis-abstract/
34•stv0g•6h ago•8 comments

Ruby 4.0.0

https://www.ruby-lang.org/en/news/2025/12/25/ruby-4-0-0-released/
461•FBISurveillance•10h ago•80 comments

Fabrice Bellard: Biography (2009) [pdf]

https://www.ipaidia.gr/wp-content/uploads/2020/12/117-2020-fabrice-bellard.pdf
308•lioeters•20h ago•92 comments

Show HN: Minimalist editor that lives in browser, stores everything in the URL

https://github.com/antonmedv/textarea
375•medv•19h ago•128 comments

Asterisk AI Voice Agent

https://github.com/hkjarral/Asterisk-AI-Voice-Agent
143•akrulino•15h ago•64 comments

Ask HN: How do I bridge the gap between PhD and SWE experiences?

15•ecophyseis•6d ago•11 comments

CSRF protection without tokens or hidden form fields

https://blog.miguelgrinberg.com/post/csrf-protection-without-tokens-or-hidden-form-fields
240•adevilinyc•3d ago•87 comments

Handheld PC Community Forums

https://www.hpcfactor.com/forums/category-view.asp
35•walterbell•3d ago•11 comments

Fabrice Bellard Releases MicroQuickJS

https://github.com/bellard/mquickjs/blob/main/README.md
1404•Aissen•1d ago•529 comments

The Fisher-Yates shuffle is backward

https://possiblywrong.wordpress.com/2020/12/10/the-fisher-yates-shuffle-is-backward/
33•possiblywrong•5d ago•11 comments

Show HN: Vibium – Browser automation for AI and humans, by Selenium's creator

https://github.com/VibiumDev/vibium
351•hugs•21h ago•103 comments

JEDEC developing reduced pin count HBM4 standard to enable higher capacity

https://blocksandfiles.com/2025/12/17/jedec-sphbm4/
51•rbanffy•6d ago•8 comments

Research team digitizes more than 100 years of Canadian infectious disease data

https://news.mcmaster.ca/mcmaster-research-team-digitizes-more-than-100-years-of-canadian-infecti...
132•XzetaU8•6d ago•6 comments

Show HN: Exploring Mathematics with Python

https://coe.psu.ac.th/ad/explore/
166•Andrew2565•5d ago•15 comments

Comptime – C# meta-programming with compile-time code generation and evaluation

https://github.com/sebastienros/comptime
114•bj-rn•4d ago•46 comments

Using Vectorize to build an unreasonably good search engine in 160 lines of code

https://blog.partykit.io/posts/using-vectorize-to-build-search/
93•ColinWright•3d ago•29 comments

Nvidia to buy assets from Groq for $20B cash

https://www.cnbc.com/2025/12/24/nvidia-buying-ai-chip-startup-groq-for-about-20-billion-biggest-d...
599•nickrubin•18h ago•338 comments

The next-gen mainboard designed with amigaos4 and morphos in mind

https://mirari.vitasys.nl/our-story/
59•todsacerdoti•14h ago•11 comments

The port I couldn't ship

https://ammil.industries/the-port-i-couldnt-ship/
130•cjlm•6d ago•82 comments

I'm returning my Framework 16

https://yorickpeterse.com/articles/im-returning-my-framework-16/
261•YorickPeterse•1d ago•448 comments