Re: legacy code retreat's triva game : the D version

2013-12-22 Thread Marco Leise
Am Sun, 22 Dec 2013 02:12:51 +0100 schrieb Timon Gehr timon.g...@gmx.ch: On 12/22/2013 02:09 AM, Timon Gehr wrote: The morale is that uniform random numbers doesn't imply that every value in the range will eventually be generated once! Yes it does. (The probability that some value is

Re: legacy code retreat's triva game : the D version

2013-12-22 Thread Chris Cain
On Sunday, 22 December 2013 at 08:06:30 UTC, Marco Leise wrote: Can you elaborate a bit? How do you know that the Java LCG can produce every 32-bit integer once? If that's true then the problem with the Java code was something different and I was just biased, because I was already expecting the

Re: legacy code retreat's triva game : the D version

2013-12-22 Thread Timon Gehr
On 12/22/2013 09:06 AM, Marco Leise wrote: Am Sun, 22 Dec 2013 02:12:51 +0100 schrieb Timon Gehr timon.g...@gmx.ch: On 12/22/2013 02:09 AM, Timon Gehr wrote: The morale is that uniform random numbers doesn't imply that every value in the range will eventually be generated once! Yes it

Re: legacy code retreat's triva game : the D version

2013-12-22 Thread Marco Leise
Am Sun, 22 Dec 2013 09:19:48 + schrieb Chris Cain clc...@uncg.edu: On Sunday, 22 December 2013 at 08:06:30 UTC, Marco Leise wrote: Can you elaborate a bit? How do you know that the Java LCG can produce every 32-bit integer once? If that's true then the problem with the Java code was

Re: legacy code retreat's triva game : the D version

2013-12-21 Thread Ivan Kazmenko
On Saturday, 21 December 2013 at 05:12:57 UTC, Chris Cain wrote: For more information, I've written a document on an implementation of uniform (which should be coming in 2.065, btw) which discusses the issue with just using the modulus operator:

Re: legacy code retreat's triva game : the D version

2013-12-21 Thread bearophile
Chris Cain: https://dl.dropboxusercontent.com/u/2206555/uniformUpgrade.pdf From page 6: size_t[] counts = new size_t[](top); foreach(i; 0 .. 500_000_000) counts[uniform(0, top)] += 1; Modern D allows you to write better code: size_t[N] counts; foreach (immutable _; 0 .. 500_000_000)

Re: legacy code retreat's triva game : the D version

2013-12-21 Thread Meta
On Saturday, 21 December 2013 at 15:03:34 UTC, bearophile wrote: Chris Cain: https://dl.dropboxusercontent.com/u/2206555/uniformUpgrade.pdf From page 6: size_t[] counts = new size_t[](top); foreach(i; 0 .. 500_000_000) counts[uniform(0, top)] += 1; Modern D allows you to write better

Re: legacy code retreat's triva game : the D version

2013-12-21 Thread bearophile
Meta: I know immutable is a good thing, but don't you think `immutable _` is a bit unnecessary in this case? Some answer, choose the one you prefer: 1) Yes, it's totally useless because the _ variable is not even used inside the loop body! So sorry, I'm always so pedantic. 2) It's

Re: legacy code retreat's triva game : the D version

2013-12-21 Thread Marco Leise
Am Fri, 20 Dec 2013 15:53:08 +0100 schrieb marcpmichel marc.p.mic...@gmail.com: I participated in the global day of code retreat 2013, and we had to do refactoring on a very ugly piece of code which was available on many languages. But there was no D version, so I made one (based on the

Re: legacy code retreat's triva game : the D version

2013-12-21 Thread Timon Gehr
On 12/22/2013 01:07 AM, Marco Leise wrote: ... It didn't cause issues for any of the other students, but on this particular computer the random seed that the Random ctor chose caused a degenerate case where it never hit any of the 3 remaining indexes of the list. The morale is that uniform

Re: legacy code retreat's triva game : the D version

2013-12-21 Thread Timon Gehr
On 12/22/2013 02:09 AM, Timon Gehr wrote: The morale is that uniform random numbers doesn't imply that every value in the range will eventually be generated once! Yes it does. (The probability that some value is never generated is 0.) The actual morale is that random number generators do not

Re: legacy code retreat's triva game : the D version

2013-12-21 Thread ilya-stromberg
On Saturday, 21 December 2013 at 20:43:27 UTC, bearophile wrote: 3) Just like the integer '5' a range of values as 0 .. 1000 is an immutable value. So a variable that scans such range should be immutable. If you really want to mutate such variable you should add a modifier like mutable or mut

Re: legacy code retreat's triva game : the D version

2013-12-20 Thread bearophile
marcpmichel: Here is the ugly thing : https://github.com/jbrains/trivia/tree/master/d And wrong: if (rand.front() % 9 == 7) { Bye, bearophile

Re: legacy code retreat's triva game : the D version

2013-12-20 Thread marcpmichel
On Friday, 20 December 2013 at 15:05:07 UTC, bearophile wrote: marcpmichel: Here is the ugly thing : https://github.com/jbrains/trivia/tree/master/d And wrong: if (rand.front() % 9 == 7) { Bye, bearophile Do you mean I should have used : if (uniform(0,10) == 7) { instead ?

Re: legacy code retreat's triva game : the D version

2013-12-20 Thread bearophile
marcpmichel: Do you mean I should have used : if (uniform(0,10) == 7) { instead ? Right. Using % introduces a bias. Bye, bearophile

Re: legacy code retreat's triva game : the D version

2013-12-20 Thread Chris Cain
On Friday, 20 December 2013 at 16:20:44 UTC, marcpmichel wrote: On Friday, 20 December 2013 at 15:05:07 UTC, bearophile wrote: marcpmichel: Here is the ugly thing : https://github.com/jbrains/trivia/tree/master/d And wrong: if (rand.front() % 9 == 7) { Bye, bearophile Do you mean I