What is the history of the GetRandomRgn function?


An anonymous commenter was curious about how the Get­Random­Rgn function arrived at its strange name, what the purpose of the third parameter is, and why it is inconsistent between Windows 95 and Windows NT.

The sense of word "random" here is hacker slang rather than its formal probabilistic definition, specifically sense 2: "Assorted, undistinguished", perhaps with a bit of sense 4 sprinkled in: "Not well organized." (Commenter Gabe suggested that a better name would have been Get­Specific­Rgn.)

Once upon a time, when men were men and Windows was 16-bit, there was an internal function used to communicate between the window manager and GDI in order to set up device contexts. Internally, the region was called the Rao Region, named after Rao Remala, the programmer who invented it, and the function that calculated the Rao Region was rather uncreatively called Compute­Rao­Rgn.

When porting to 32-bit Windows, the Windows NT and Windows 95 teams both found that they needed this same internal communication between the window manager and GDI. GDI already has a bunch of functions named Get­Xxx­Rgn, so instead of writing a separate marshaler for each one, they opted to write a single Get­Random­Rgn function which takes an integer which serves as a function code, specifying which region the caller actually wants. (I suspect the Windows 95 team followed the cue of the Windows NT team, since Windows NT ran into the problem first.)

Since this was an internal function, it didn't matter that the name was a bit cutesy, nor did it matter what coordinate system it used, as long as the window manager and GDI agreed on the name and coordinate system. The Windows 95 team still had a lot of 16-bit code that they needed to be compatible with, so they chose to generate the Rao region the same way that the 16-bit Compute­Rao­Rgn function did it. The Windows NT folks, on the other hand, decided that it was more convenient for them that this internal function use screen coordinates, so that's what it returns on Windows NT.

Get­Random­Rgn isn't really a function that was designed to be public. It was just an internal helper function that outsiders discovered and relied upon to the point that that it became a compatibility constraint so strong that it turned into a de facto documented function. And all the weirdness you see behind it is the weirdness of a function never intended for public consumption.

The introduction of the Desktop Window Manager in Windows Vista changed the way the visible region was managed (since all windows are logically visible even when occluded because their drawing is redirected to an off-screen surface), but the Get­Random­Rgn function has to keep track of the "visible region" anyway, for compatibility.

Comments (26)
  1. JM says:

    I wish a programmer named Rao worked at my company so I could exclaim "Great Rao!" every so often. (For those not in on the joke, Google it.)

  2. 640k says:

    In the same way there's nothing random about RAM.

  3. Bob says:

    Re 640k:  Exactly….  I would think this is "random" as in en.wikipedia.org/…/Random_access  which is a definition not well supported by the "Hacker dictionary".  And it dates from a time when men were men and computers didn't necessarily have operating systems.

  4. jmthomas says:

    From the function's point of view, the parameter value is a random selection (by the caller) from the set of values documented for the parameter.

    "Random" isn't a hacker term — it is a systems analysis term, meaning that the value of a property may be anything within defined limits.

    "Arbitrary" is a good synonym this usage of "random".

  5. Anonymous Coward says:

    ‘the Get­Random­Rgn function has to keep track of the "visible region" anyway’ – There isn't much ‘to keep track of’ though.

    Also, random is a horrible word in certain contexts, because there can be a complete ambiguity between the meaning of ‘you can pick any value you like’ and ‘might return any value, beyond your control or prediction’. This is especially troublesome in game rules, where I've seen it used in both senses.

  6. Anonymous says:

    Thanks for answering my question Raymond. I'm still enjoying your blog every day.

  7. voo says:

    Not the first time that "random" is abused in such a way. Seeing "public static final int RANDOM_PRIME = 33" is quite confusing until you notice that some team members just don't get the difference between "arbitrary" and "random".

  8. SimonRev says:

    @Voo

    Can I still find it disturbing that someone there thinks that 33 is prime?

  9. Ian Yates says:

    @SimonRev – haha :)

    It's funny how odd numbers are elevated to prime automatically by some.  I guess it ends in 3, so part of it is prime? :P

    @Voo et al – I agree, arbitrary is a much better term.  My sister (26yo) also uses the term "random" in place of "stranger".  EG: "Some random person just turned up at the party" which is often shortened to "who's that random over there?".   Having a maths & IT degree it took me a moment to work it out :)

  10. "Random" is also used by some people to mean "evenly distributed".

    "Random" is also used by some people to mean "bizarre" / "strange".

  11. voo says:

    @SimonRev Noticed that as soon as I'd clicked "post" (but hey, at least the blog software worked perfectly for once), but then it was too late and I thought maybe nobody would notice it. Obviously two serious errors on my part there ;)

    I'll just go into that corner over there.

  12. cheong00 says:

    @HiTechHiTouch: I'm thinking exactly the same thing when reading this, talking about the possible usage of "Arbitrary" here.

  13. dave says:

    You mean GetRandomRgn *doesn't* return region #4?

    http://xkcd.com/221/

  14. Neil says:

    The Rao region sounds like it was the one returned by the (also undocumented) InquireVisRgn function that I abused in a window capture utility because I didn't want to try to capture obscured or off-screen pixels.

  15. ThomasX says:

    @SimonRev

    33=3*11. So it's actually two primes in one.

  16. dave says:

    There seems to be a lot of agony in this thread from people who are disturbed to find that some English words can have more than one meaning, possibly colloquial.

  17. @dave

    I think the agony here is about this particular use of random, not that spoken languages can have multiple meanings, homonyms or words having informal meanings. The naming of this function implies something that it actually do, and it could be even worse for people who aren't native English speakers.

  18. SimonRev says:

    @Dave my take on it is more that Random has a specific meaning in the domain of computer software that is related to pseudorandom number generation, and not arbitrary.  Therefore programmers tend to get a little confused when they hear other programmers use different usages of the word.

    By similar example, my brother is a lawyer.  He is greatly agonized whenever anyone mixes up slander and libel.  Most Americans will interchange the two freely, even though to a lawyer they have distinct meanings.  If another lawyer started mixing the meanings I can see where he would be confused and frustrated.

    [I think you'll find in computer software circles, the word random has extended non-mathematical meanings as described in the Jargon File. "The user can grab any random file." This is the word "random" in the sense of "arbitrary" rather than "statistically random". -Raymond]
  19. dave says:

    Sure, I think the word 'Random' in GetRandomRgn could have been better chosen, but, hey, if we can cope with 'ANSI Code Page' being used for a non-ANSI code page …

  20. dave says:

    I can of course take both sides of this argument.  As a programmer, I get confused and frustrated when people mix up 'permissions' and 'privileges'.  

  21. James says:

    "I wish a programmer named Rao worked at my company so I could exclaim "Great Rao!" every so often."

    No need to Bing/Google whatsoever, and +1 for the reference. :)

  22. voo says:

    [I think you'll find in computer software circles, the word random has extended non-mathematical meanings as described in the Jargon File. "The user can grab any random file." This is the word "random" in the sense of "arbitrary" rather than "statistically random". -Raymond]

    Sure, but that's a colloquial usage. I think we are generally more careful with semantics for documentation and variables. Other example: I'm totally fine using "GB" when actually meaning "GiB" in a normal discussion, but if I'm writing some documentation for networking code I sure as hell will make sure to either use the correct, standardized meaning – or, if it's existing, older code – to make sure it's clearly documented that yes when saying "GB" we actually mean 2^30 bytes.

    [The function was not intended to be public or documented. It was named based on the colloquial meaning. -Raymond]
  23. alegr1 says:

    Who would have thought that Random House is where they publish books.

  24. Gabe says:

    The funny thing about the way programmers use the term "random" is that of all the things it means ("arbitrary", "pseudo-random") it almost never means "actually random"!

  25. Anonymous Coward says:

    @Dave: It's just no fun to come across a situation where it's impossible to tell whether you should pick a card face down or face up. The problem is not the multiple meanings as such, the problem is that it has so many meanings, many of which completely opposite, and impossible to tell apart from context. The word random has almost lost its meaning, to the point where you can just blot out all its occurrences and hardly lose any information at all. But at least then you know something's missing. But when there's a word there, those six letters instruct all readers that they know what it means, even though none of them do.

  26. Horst Kiehl says:

    "The word random has almost lost its meaning".

    So the meaning of "random" is just random now.

Comments are closed.

Skip to main content