Jump to content
Sign in to follow this  
kote eclair

Random number generator

Recommended Posts

Some people complain about "unfairness" of random: events with low probability (such as a dungeon mob dropping a gem) tend to happen rarer than common probability understanding suggests (N.B.: I'm not saying it is correct). I myself remember countless hours of farming FC1 (and, later, DW2) eating luck amplifiers and not getting any refining gems at all, while my friends could easily get 5+ refs after farming for an hour or two. This made me interested in randomness in TOP/PKO server.

 

I found out that server .lua scripts use function Percentage_Random for all randomness:

function Percentage_Random(a) 
	local x,y=a*1000000000,math.random(0,1000000000) 
	local z 
	if y<=x then 
		z=1 
		else z=0 
	end 
	return z 
end 

The source of randomness in Percentage_Random is math.random function, which originates from CaLua library (written in C):

static int math_random (lua_State *L) {
  /* the `%' avoids the (rare) case of r==1, and is needed also because on
     some systems (SunOS!) `rand()' may return a value larger than RAND_MAX */
  lua_Number r = (lua_Number)(rand()%RAND_MAX) / (lua_Number)RAND_MAX;
  switch (lua_gettop(L)) {  /* check number of arguments */
    case 0: {  /* no arguments */
      lua_pushnumber(L, r);  /* Number between 0 and 1 */
      break;
    }
    case 1: {  /* only upper limit */
      int u = luaL_checkint(L, 1);
      luaL_argcheck(L, 1<=u, 1, "interval is empty");
      lua_pushnumber(L, (int)floor(r*u)+1);  /* int between 1 and `u' */
      break;
    }
    case 2: {  /* lower and upper limits */
      int l = luaL_checkint(L, 1);
      int u = luaL_checkint(L, 2);
      luaL_argcheck(L, l<=u, 2, "interval is empty");
      lua_pushnumber(L, (int)floor(r*(u-l+1))+l);  /* int between `l' and `u' */
      break;
    }
    default: return luaL_error(L, "wrong number of arguments");
  }
  return 1;
}

So, it seems that all randomness in Tales of Pirates got its roots in rand() from stdlib.

 

As far as I know, the C standard does not prescribe specific pseudorandom number generator (PRNG) implementation. The game was made in mid 2000s, and PRNG implementations used in stdlib in these times might have been faulty (not returning uniformly distributed numbers, which is crucial).

 

The question is: what is the PRNG a TOP/PKO server executable uses? Did anyone ever explore this case?

Edited by kote eclair
  • Like 1

Share this post


Link to post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Sign in to follow this  

×
×
  • Create New...