Menu

#233 Rand() implementation in 2.5x and 2.6x

v2.6x
open
nobody
rand (1)
8
2017-04-04
2017-04-04
No

I am simulating pseudo random number in AviSynth script, but seed is controlled by myself to keep stable pseudo random result.
So I watched Rand() implementation in script.cpp, try to make my random function behavior can be almost same as AviSynth buint-in.
And I found that code is somewhat strange.
Is it means that default max is 32767 (and possible return value is from 0~32766)?
Also, scale mode is enabled when max == 32768?
If it is, it might be different from details written in AviSynth official document.

AVSValue Rand(AVSValue args, void user_data, IScriptEnvironment env)
{ int limit = args[0].AsInt(RAND_MAX);
bool scale_mode = args[1].AsBool((abs(limit) > RAND_MAX));

if (args[2].AsBool(false)) srand( (unsigned) time(NULL) ); //seed

if (scale_mode) {
double f = 1.0 / (RAND_MAX + 1.0);
return int(f * rand() * limit);
}
else { //modulus mode
int s = (limit < 0 ? -1 : 1);
if (limit==0) return 0;
else return s * rand() % limit;
}
}

Thanks.

Discussion


Log in to post a comment.

MongoDB Logo MongoDB