PenUltima Online Forum Index Official Core: 096.7
Official Core: 097 2008-02-26
Donate towards the POL web hosting bill!
 POL Home   FAQ   Search    Memberlist   Usergroups    Register    Profile   Log in to check your private messages   Log in
randomint 'problem'

 
Post new topic   Reply to topic    PenUltima Online Forum Index -> Bug Reports 097
Display posts from previous:   

Author Message
phao



Joined: 31 Aug 2007
Posts: 34

PostPosted: Fri Mar 07, 2008 3:13 pm    Post subject: randomint 'problem' Reply with quote

i have this line of code
Code:
var hair := cint(race_hair_list[randomint(race_hair_list.size()) + 1]);


and 'race_hair_list' is an array of hair ids

it seems that the randomint() is returning the same number everytime. this line of code is called everytime a character is created.

good observations:
- it isn't evertyime, just everytime for the first char created
- so the second char i'll have a diferent hair, and it is the same hair everytime.
- so it seems that for each char it is returning an 'equal' number.

is that a bug? i think it is.

i've heard about somethine like seed() (an C function), and i dont know how, it modifies the base variable the random functions use to generate random numbers, is that something like that in POL?

is that really a bug?

Author Message
phao



Joined: 31 Aug 2007
Posts: 34

PostPosted: Fri Mar 07, 2008 3:57 pm    Post subject: Reply with quote

well, sorry flood, beeing or not a bug, it's a problem to me, it seems that the randomint() returning values are 'predefined' (i doubt they really are).

i've done something to 'avoid that'.

in the start.src i have

Code:
use os;

start_script("fix_random_bug", 10);


in the fix_random_bug.src i have
Code:
use util;
use os;

program fix_random_bug(val)
  while(1==1)
    randomint(val);
    sleepms(500);
  endwhile
endprogram


after doing that, the results are beeing different from the 'everytime the same', each time i start the server and recreate a lot of char, i'm getting different hairs.[/code]

Author Message
Xpatriat



Joined: 04 Aug 2007
Posts: 7

PostPosted: Tue Mar 11, 2008 2:34 pm    Post subject: Reply with quote

You are absolutely correct; The random generator in POL is likely very deterministic - meaning it probably does literally give the same series of values every time. You mentioned a 'seed'. A typical non-cryptographic random generator typically takes a 'seed' number, and then it produces an EXACT, predictable series of values based on the seed. Usually this is OK, because no one really knows when individual calls to the random function are going to be made. But POL also seems to have some issue with the values per-thread, or something similar.

I'm intrigued that your solution seems to work, though. That's actually an intuitive solution. You might also increase the 'randomness' of it by having each value modify the sleep time slightly - maybe take RandomInt(10), multiply by 10, and add the value to 450 for the sleepms time. That might add to the 'perception' of randomness of the values.


For those wondering; the reason typical 'random' generators are not really random is because a truly 'nearly-random' number is near impossible for a computer to generate - and it often takes time to do it, because it has to try to get some values from human or environmental variables over time. The best a computer can do otherwise is try to generate numbers that 'feel' random when viewed across time.

Author Message
OldnGrey



Joined: 04 Feb 2006
Posts: 517

PostPosted: Sun Apr 20, 2008 3:33 am    Post subject: Reply with quote

Amazing really.
I occasionally get players complaining to me about the way the dice outcomes effect them.

I had a script that simply said if ( randomint(6) == 0 ) then do something. I'd be expecting that to pass 1 in 6 times, but it's more like 1 in 30. Because changing it to test for a 1 does work around 1 in 6.

I wonder just how many of our scripts are not giving the results we expect because of random numbers?

Using things like the last 4 digits of readgameclock as a seed also appeals to me somewhat. I've put Phao's script in my shard just in case it's going to stop my players complaining, but wonder if anyone else has some thoughts on taming randomint?

Author Message
Luth
POL Developer


Joined: 30 Jan 2006
Posts: 35

PostPosted: Mon Apr 21, 2008 8:22 pm    Post subject: Reply with quote

Realize that Random() is NOT an even distribution of numbers. You can do RandomInt(6) ten times in a row and get six 1s, and it is still "random." In fact, if you did have guaranteed statistical evenness then it wouldn't really be random; you'd be able to predict which number would come next. Statistical probabilities are ONLY valid for sufficiently large quantities (hundreds, thousands, or millions of test cases). Anything less is nearly meaningless.

Imagine it like this: There is an infinite sequence of numbers in a list. RandomInt pulls the "next" value and returns it. If you start at the head of the list [0] every time, the sequence will be the same every time. Makes sense, right? Thats where "seeding" comes in. Setting a "seed" is like setting a position in the list to start reading numbers at. Thus you need a random / psuedo-random outside value to seed with.

Phao's fix works because there is a psuedo-random outside value (the time until a "real" randomint call is made) which results in varied previous numbers of times randomint was called, thus psuedo-seeding the random generator. He may be pulling the same sequence of numbers every time, but a random amount of those numbers are discarded before one is accepted.

From a programming point of view, OGs fix is the best; seeding the random number generator with a psuedo-random outside value (in this case, the game clock value [the bigger range the seed, the better]). Its a do-once action that will result in different random sequences (nearly) every time you use it (and it doesn't require another process to keep running Wink ).

Author Message
OldnGrey



Joined: 04 Feb 2006
Posts: 517

PostPosted: Mon Apr 21, 2008 8:55 pm    Post subject: Reply with quote

In fact Phao's suggestion really does work.

I put it in and didn't tell most players. I am getting reports that things really do appear to be more random - in fact things are appearing at the rate the RandomInt number is suggesting. So much so that I am having to tone down the appearance of rare items in the fishing script.

The theory of how RandomInt works is useful to know, but what's more useful is to be able to just code without wondering any more.

I hope this isn't on a per thread basis, so far it seems to be okay.

Author Message
Luth
POL Developer


Joined: 30 Jan 2006
Posts: 35

PostPosted: Mon Apr 21, 2008 11:17 pm    Post subject: Reply with quote

I didn't say it wouldn't work; I gave reasons why it would. Smile The more you can find a way to add randomness, the better.

Author Message
Yukiko



Joined: 02 Feb 2006
Posts: 1094
Location: Southern Central USA

PostPosted: Tue Apr 22, 2008 3:02 pm    Post subject: Reply with quote

For some reason I seem to get a better "random" distribution when I use RandomDiceRoll() rather than RandomInt(). Not sure why. Maybe it's just my perception but I set-up a script using both and RandomDiceRoll() appeared to do better.

Author Message
Luth
POL Developer


Joined: 30 Jan 2006
Posts: 35

PostPosted: Tue Apr 22, 2008 7:32 pm    Post subject: Reply with quote

How many thousands of times did you test? If you only tried a couple dozen, or a couple hundred, you completely miss the usefulness of statistics. Wink

Still, though, if someone reminds me, I'll look at the source and see if there is a code-wise difference between the two functions.

If anything is wonky, I think I'll replace the code with this:
Code:
unsigned int RandomInt(unsigned int max)
{
  return 6;  //this number was selected by a dice roll and is therefore completely random
}

Author Message
OldnGrey



Joined: 04 Feb 2006
Posts: 517

PostPosted: Tue Apr 22, 2008 8:35 pm    Post subject: Reply with quote

In the "real world" of doing scripts for our game, all we want is to throw a dice once and use the outcome. My poor suffering players are rarely comforted with things like "in the long run it's random" when they get 4 fails in a row at supposedly 50% odds.

I always get reports from players that number sequences go in bunches - ie it almost appears like the mythical losing streak. I even got a complaint last night that they were winning TOO much. sheesh.

For my latest trick I am now going to replace things like
if ( RandomInt(2) == 1 )

with
if ( RandomInt(100) < 50 )

Sounds like it will have more of a chance to 'appear' random.

Author Message
ncrsn



Joined: 10 Feb 2006
Posts: 168

PostPosted: Wed Apr 23, 2008 6:57 am    Post subject: Reply with quote

OldnGrey, if your players want predictable randomness and you are willing to give that to them, why are you sticking with random number generator so much?

I didn't test this, but I think it's worth a look.

Code:

function Random( number, byref curvalue )
   
    if (curvalue == error)
        curvalue := RandomInt(number);
    else
        curvalue += 1;
    endif
   
    if (curvalue >= number - 1)
        curvalue := 0;
        return 1;
    endif
endfunction


Usage could be like this.

Code:

    // Player has a 1/4 chance to get a congratulation message
    var curvalue := GetObjProperty(who, "#CongraDice");
   
    if (Random(4, curvalue))
        SendSysMessage(who, "Congratulations!");
    endif
   
    SetObjProperty(who, "#CongraDice", curvalue);


This isn't an ideal script: one would likely integrate the cprop part into (another) function to ease the use. And if repeated this isn't really random at all, so someone could find this even boring.

--

Some years ago I did test the POL's randomness using RandomInt(). I couldn't find the data anymore, so you'll have to take my word for it, but it was surprisingly random when there were 10 000, 100 000 and even million throws. Surprisingly, because I, too, had heard the "POL's random generator sucks!" lines.

I never did test the difference between RandomInt, RandomDiceValue and RandomFloat, because I always thought they were basically the same.

Author Message
Luth
POL Developer


Joined: 30 Jan 2006
Posts: 35

PostPosted: Wed Apr 23, 2008 8:15 pm    Post subject: Reply with quote

OG, it sounds like you want non-random returns, then. (And, thus, not a "random problem" Cool) If you want low-test statistical accuracy, then write your own function:

Create a string "Values":
"00101001100110101110"

function GetFiftyPercent() would look something like:
{
var idx := GetGlobalProp("fiftyIdx");
var val := Values[idx];
SetGlobalProp("fiftyIdx", (idx+1)%(strlen(Values));
return CInt(val);
}

This will give you pure statistical accuracy over a very small (20) trial set. If you make the string long enough that it cant be memorize, but small enough that you can ensure high statistical accuracy over a small trial set, then that should make them happy, right?

(ps: thats psuedo script, dont go copy/pasting and expecting it to work.)

(pps: If you bet on black every time, seeing five reds in a row does not mean that the randomness of physics is broken. Wink )

Author Message
Pierce



Joined: 02 Feb 2006
Posts: 256

PostPosted: Thu Apr 24, 2008 10:50 am    Post subject: Reply with quote

A few years ago i also thought of a randomint "problem or bug". Therefor i wrote a small textcmd script (.runrandom) to check it. Here the 97 code of it:


Code:

use uo;
use os;
use util;

program randomstatistics( who, stopcounter )

 var counter := 0;
 var temp;
 var statisticarray := {0,0,0,0,0,0,0,0,0,0};


 while (counter != CInt(stopcounter))
  temp := RandomInt(10);
  temp += 1;
  statisticarray[temp] += 1;
  counter += 1;
  sleepms(1);
 endwhile
 SendSysMessage(who, "Statistics: " + statisticarray); 

endprogram


Sure it can also be done with other than a 10 % randomint.
If you e.g. type .runrandom 10, which means it runs 10 times, you could possibly think of a "bug".
But to get a visible view of what Luth means, just try .runrandom 100000.
Nearly all values are around 10.000 this time, which is a good statistical outcome Very Happy Surely they are not exact 10.000 cause its random Very Happy

Post new topic   Reply to topic    PenUltima Online Forum Index -> Bug Reports 097 All times are GMT - 4 Hours
Page 1 of 1

 




Powered by phpBB © 2001, 2005 phpBB Group :: Theme & Graphics by GHS & Scott E. Royalty