std.string and ranges
bearophile
bearophileHUGS at lycos.com
Wed Feb 11 10:00:14 PST 2009
Andrei Alexandrescu:
>I don't know how to select a random element of an infinite range or one that has an unknown length.<
The programmer has to take care of not giving an infinite range, because it may lead to an unbounded time to wait.
If the range is finite, but its length isn't know, there are algorithms to extract an item randomly with uniform probability.
(Do you remember your quiz to extract a random line from a file, where at the beginning you don't know how many lines it has?)
>Well this becomes nitpicky.<
You are right, I am sorry, I was getting a bit nervous. You are usually gentle with me, and this helps me think much better. Maybe this time you are nervous/angry because I have not read the docs of sts.random of Phobos of D2.
> double d = uniform(rnd, 0.0, 1.0);
> The "uniform" function infers the type of its result from its arguments.
If you have forgotten to put a .0 there in a small example, then it looks a bit too much bug-prone. I think I prefer two functions still, it's also useful as self-documentation of the program.
>Where's the underlying random generator?<
If not specified it uses the default non-thread-safe generator (something like KISS in my suggestions, that is the middle one).
>Saving a line of code doesn't seem convincing.<
It's not a line of code, shuffled() is an expression, so you can put it inside other expressions, it may save more than one line. One line here and one line there make the difference.
Having only functionally atomic functions/things is useful because you can combine them, but when you program you may see that 80% of the times you want to combine then in small amount of certain specific way. So it's positive to add such common idioms as handy functions (that can also be used as an expression too).
>Well it would be good if you read the documentation,<
I have taken a look at the docs for dice(), I don't like its name because isn't intuitive at all, but its usage is easy. The usage of the function I have suggested is a bit more higher level.
An possible alternative design for such function is to take in input an already sorted array of the weights (beside the iterable of the items), this may speed up this function a bit (it just needs to call the algorithm for bisect search, I presume).
>Well it looks like everyone thinks their favorite generator is the "best".<
I have suggested three generators because there's no "best": the numbers generated by R250/521 are worse than ones generated by KISS and much worse than ones generated by Mersenne.
> http://open-std.org/jtc1/sc22/wg21/docs/papers/2007/n2461.pdf
>, which goes for defining several generators.
I have read about R250/521, and I have used it in some simulations in C/D, with good/acceptable results. But I can't "compete" with groups of people that define C++. Among them there are probably people expert about random generators, so it may be wiser for you to follow them and to ignore me.
Regarding C++, if not already present, the default sort of D arrays may become the stable sort of some good C++ STL.
Bye,
bearophile
More information about the Digitalmars-d
mailing list