48 points · by ingve · 4y ago · 34 comments
(Sadly the common flavour of EPP has a bonkers data model; a few ccTLDs use an alternative model with a more sensible data model, but although the more sensible flavour of EPP is easier to use in isolation, it has the downside of being different from the ICANN flavour.)
Is it .at blocking the nameserver changes or AWS refusing to serve .at domains from their nameservers?
Find the largest multiple of your limit which is less than W. We want to find a random number less than this multiple, because then we can just divide down to get an unbiased result. Maybe this is already implied in how these algorithms are set up, not sure.
Generate a candidate random number. If it is smaller than the multiplied limit, you are done. If not, double it, and add a random bit. Repeat until success.
To supply the random bits, generate a random number, and use it up one bit at a time. If you need more, generate another random number.
I suppose this doesn't work, because the second-most-significant bit of a rejected candidate isn't unbiased. If the multiplied limit is 3/4 of W, then we only reject candidates where the top two bits are both 1!
Can you rescue this by discarding more than one bit when you reject?
Probably not. Think about the case where the multiplied limit is W-1. The only candidate you would reject is all 1s. You'd have to discard all the bits for the next candidate to be unbiased.
Oh well.
The only time I hear about 'other ways' is here on Hacker News.
edit: range notation
Those examples are all relatively low-level. Many high-level languages provide a floating point interface on top of such an underlying integer pseudo randomness algorithm. This is the reason why much high-level code use multiplication to get a ranged value.
This post goes into some detail about how the V8 JavaScript engine creates a [0, 1) floating point pseudo random value from its underlying integer pseudo-random algorithm: https://v8.dev/blog/math-random
You'd think so but it doesn't really say. It's almost entirely about how they make the integer.
Following the commit link shows that they use the method of filling 1.0 with random mantissa bits and then subtracting 1.0
>> The number of random values it can generate is limited to 2^32 as opposed to the 2^52 numbers between 0 and 1 that double precision floating point can represent.
Not really true. That's how many numbers their algorithm returns, which is almost as many evenly-spaced floats there are between 0 and 1. But because it starts with a number between 1 and 2, that method actually wastes a bit, and that number really should be 2^53.
But floating point itself has 1/4 of its values between 0 and 1, so for double precision that's roughly 2^62!
Really? I would have expected at least a 20:1 ratio in favor of [0,1) over (0,1].
Most obviously, (0,1] can't produce a uniform distribution, because the input value 1 produces its own unique output value. Whereas the input value 0 shares its output value with the rest of the interval (0, 1/n).
My point is you're multiplying a float, not dividing an integer.
That's not so easy to do; I suspect the fancier methods of generating random numbers are of interest because they have an easier requirement such as "32 random bits".
I think this is likely due to the nature of JavaScript’s whacky integer and float implementations.
Java, Javascript, Python, Ruby, Perl
languages where it's optional:
C#, Elixir, Go
That's about when I got tired of Googling.
Java: Random.nextInt(b)
Python: random.randrange(b)
Ruby: rand(b)