legacy code retreat's triva game : the D version

marcpmichel marc.p.michel at gmail.com
Sat Dec 21 05:53:12 PST 2013


>> Do you mean I should have used :
>> if (uniform(0,10) == 7) {
>> instead ?
>
> TL;DR version:
> Actually, the equivalent would be uniform(0, 9), but yes, 
> that'd be the preferable approach there. (also note 
> https://github.com/jbrains/trivia/blob/7b473f9fbbd125b0ab1c2e82582b8a8c414ca501/d/source/trivia.d#L19 
> too should be changed to `uniform(1, 6)` which will give 
> numbers in the range [1 .. 6) ... that's what you want, right?)

Indeed, your're right, thanks.

I used the modulo trick for multiple reasons :
* I ported the java source, which used the basic 
java.util.random's Random.nextInt() then a modulo to cap the 
output.
* D's std.random had me scratching my head for minutes; like : 
"What is this mess ? And where is the simple rand() function ?"
* I didn't care about speed or uniformness of the generated 
numbers.
* While in the code retreat event, we tried to get a "golden 
master" ( the output of the program ), to be able to test that 
refactoring didn't change anything. One trick is to set the seed 
of the random number generator to guarantee we always got the 
same dice rolls. And the std.random complexity didn't help to 
choose the right method.

That being said, there are worse things in game.d : I introduced 
new bugs in this already buggy program, by using D's array slices.
https://github.com/jbrains/trivia/blob/7b473f9fbbd125b0ab1c2e82582b8a8c414ca501/d/source/game.d#L101

Lastly, this tiny contribution is just a drop in the ocean of 
"spreading the world about D".


More information about the Digitalmars-d-announce mailing list