Source code: curious rand() usage?

dreyk

Member
Sep 19, 2018
6
3
42
OK, not much a question about the game itself, but this is the first place I've seen this and I can't stop thinking about it (example from TerranTreats.as):
var newSkin:String = "tan";
if(rand(6) == 0) newSkin = "fair";
else if(rand(5) == 0) newSkin = "pale";
else if(rand(4) == 0) newSkin = "olive";
else if(rand(3) == 0) newSkin = "dark";
else if(rand(2) == 0) newSkin = "ebony";

I'd expect a switch or this instead:
var r:int = random(6);
if(r == 0) newSkin = "fair";
else if(r == 1) newSkin = "pale";
else if(r == 2) newSkin = "olive";
else if(r == 3) newSkin = "dark";
else if(r == 4) newSkin = "ebony";

It's like "Did we roll a 6 on that D6? No? Then roll a D5 to see if we get a 5"

Is that an Actionscript convention, something for not keeping temp vars maybe? A funny thing that got started as a curiosity? Or is there no significance?
Took me a good twenty minutes to realize it did add up to the same odds, so if the purpose was to generate WTFs then it is successful ;)
 

Karretch

Well-Known Member
Aug 26, 2015
2,063
301
Sorta, but no. It's "choose number between 0 and X, check each number in ascending order to check if match"
 

dreyk

Member
Sep 19, 2018
6
3
42
Thanks, but I meant the multiple rerolls (calls to rand).

I know the result is the same (though less efficient), of course:
gives 1/6 for "fair", 1/5*5/6 for "pale" (rand(5)==0 chance times failure to roll 0 before), 1/4*4/5*5/6 for "olive", 1/3*3/4*4/5*5/6 for "dark"...

To get back to the dies analogy: If it didn't roll a 6, you roll *another* die (D5) to see if a 5 came up. If it didn't, roll a D4 and ask if 4, and so on.
All the numbers are on the first die, though, you can roll it and ask "which number came up" :)
 

Not So Anonymous

Active Member
Jun 14, 2016
44
19
Yeah, that does seem a little odd. One would think a single die roll and a table lookup would be enough for this. This code seems like it would be better (the array could be defined outside the function, since it's never going to change at runtime):

Code:
const humanSkinTones: Array = ["tan", "fair", "pale", "olive", "dark", 'ebony"];
// Get random index into `humanSkinTones`
const index: int = rand(humanSkinTones.length);
return humanSkinTones[index];

I'm not an expert on ActionScript, but I think it supports local type inferencing, so you can probably leave off the type annotations since we're immediately initializing the variables.
 
Last edited:

DrunkZombie

Well-Known Member
Moderator
Apr 12, 2018
1,564
877
Minnesota, USA
Action script 3. (ninja'd)

Yes, that is not the way I would write it. A single Rand check and an if block or switch is more efficient. Of course this is text game code and efficiency isn't as important as it typically is in a program.

It does seem like it was written this way just because it could be.
 

Not So Anonymous

Active Member
Jun 14, 2016
44
19
I think the array code I posted is better, just because it still works if you add (or remove) a new item to the array without any other code changes. The efficiency gains by going branchless are minor in comparison to the maintainability gains.

Also, it's more likely written the way it is because Fen didn't know better at the time, and no one has the time to go around rewriting code that still works.
 
  • Like
Reactions: Wsan

dreyk

Member
Sep 19, 2018
6
3
42
Ah, thanks, all :)
Not my aim to criticise, by no means would I criticise the internals of a such an enjoyable and well-written game, it's just that not knowing for ure was bothering me :)

I mean, it's like something out of a programming interview, "does this work as intended?"
Yes, against initial impressions.
 
Last edited: