Source code from Beta 1.1 (exactly the same as up to at least 1.12, and undoubtedly also 1.21; as much as the game has changed there is a lot that hasn't changed); this means to take a random number from 0-2 (3 distinct values), add one to it (so 1-3) then roll another random number ranging from 0, 0-1, or 0-2 and add that to 2 for 2-4 with a non-uniform distribution (the chance of 4 is 1/9 or 1/3 * 1/3):
int l1 = 2 + random.nextInt(random.nextInt(3) + 1);
Note that the Wiki goes on to say "Rare taller sugar canes can be found if the world generator places two smaller canes on top of each other" but that is extremely unlikely since there must be water adjacent to the block it is placed on, which could only ever happen if it was next to a waterfall (contrast with cactus, whose only restriction is that there must be no solid blocks next to it, and this behavior is considered to be a bug so they might eventually patch it, by excluding cactus as a valid block during world generation).
Notably, my first world has a 4 block tall sugar cane near spawn (-92, 236) and I wouldn't have thought anything of it back then, given there are more relatively close by:
I got a 4 block tall sugar cane generated in my survival world is this rare?
It's not rare; I see it rather commonly.
Maybe sugar cane of five or taller would be rare?
Yes, 4-block-tall sugar cane is a fairly common sight with enough exploring; it's just not as common as the other heights. According to the Wiki:
Taller sugar cane is possible but only occurs when shorter ones generate on top of each other.
This has been common ever since sugar cane was added:
Source code from Beta 1.1 (exactly the same as up to at least 1.12, and undoubtedly also 1.21; as much as the game has changed there is a lot that hasn't changed); this means to take a random number from 0-2 (3 distinct values), add one to it (so 1-3) then roll another random number ranging from 0, 0-1, or 0-2 and add that to 2 for 2-4 with a non-uniform distribution (the chance of 4 is 1/9 or 1/3 * 1/3):
Note that the Wiki goes on to say "Rare taller sugar canes can be found if the world generator places two smaller canes on top of each other" but that is extremely unlikely since there must be water adjacent to the block it is placed on, which could only ever happen if it was next to a waterfall (contrast with cactus, whose only restriction is that there must be no solid blocks next to it, and this behavior is considered to be a bug so they might eventually patch it, by excluding cactus as a valid block during world generation).
Notably, my first world has a 4 block tall sugar cane near spawn (-92, 236) and I wouldn't have thought anything of it back then, given there are more relatively close by:
TheMasterCaver's First World - possibly the most caved-out world in Minecraft history - includes world download.
TheMasterCaver's World - my own version of Minecraft largely based on my views of how the game should have evolved since 1.6.4.
Why do I still play in 1.6.4?