OpenBOR v3.0 Build 4078 for Windows/Linux/Wii

Status
Not open for further replies.

DCurrent

Site Owner, OpenBOR Project Leader
Staff member
New script command:

srand({seed}) - Set global random number generation seed to {seed}. In most cases (probably all) you'll want to use elapsed time.

Normally, random numbers are seeded with an incrementing default starting at 1234567890. See below:

Code:
/*
 * OpenBOR - http://www.chronocrash.com
 * -----------------------------------------------------------------------
 * All rights reserved, see LICENSE in OpenBOR root for details.
 *
 * Copyright (c) 2004 - 2014 OpenBOR Team
 */

#include "rand32.h"
#include "types.h"

unsigned long seed = 1234567890;

unsigned int rand32(void)
{
    u64 t = seed;
    t *= 1103515245ull;
    t += 12345ull;
    seed = t;
    return (t >> 16) & 0xFFFFFFFF;
}

void srand32(int n)
{
    seed = n;
}

Note: This feature is not yet tested. Also be aware the random number seed is global, so ALL subsequent random number generation calls will be affected, including those made internally by the engine for things like AI randomness. While it should actually improve AI diversity, there may be unforeseen side effects.

DOWNLOAD
 
oh yeah!
so now, instead of
Code:
   if(frame==0){
      void vSelf = getlocalvar("self");
      int r = rand()%30;

      if(r > 0){
        changeentityproperty(vSelf, "animation", openborconstant("ANI_ATTACK4"));
      }
    }

we should do this?
  if(frame==0){
      void vSelf = getlocalvar("self");
int time = openborvariant("elapsed_time");
      int r = srand(time)%30;

      if(r > 0){
        changeentityproperty(vSelf, "animation", openborconstant("ANI_ATTACK4"));
      }
    }
 
No, srand() sets the global random seed. You still need to call rand() to actually get a random number.

Code:
if(frame==0){
     void vSelf = getlocalvar("self");
     int time = openborvariant("elapsed_time");
     
     srand(time);
 
     int r = rand()%30;

     if(r > 0){
          changeentityproperty(vSelf, "animation", openborconstant("ANI_ATTACK4"));
     }
}

Edit: There's no reason I couldn't make srand() go ahead and return a random number, and probably will tomorrow. I'm done for tonight though. ;)

DC
 
ah, thanks.
and can we add this to track the results?
Code:
if(frame==0){
     void vSelf = getlocalvar("self");
     int time = openborvariant("elapsed_time");
     
     srand(time);
 
     int r = rand()%30;
     log(time);
     log (r);
     if(r > 0){
          changeentityproperty(vSelf, "animation", openborconstant("ANI_ATTACK4"));
     }
}

edit: the link isnt working
 
Yes you can do that, though you might want to do something like log(r + '\n') to make it readable. What link is broken.. download? It works fine for me.

DC
 
Great work , I guess using it on android will crash/shutdown the engine unless someone compiles it ?
Could you modify the code so when you're calling "rand" in old mods then youre acutally calling new "srand" and it works fine without having to setup elapsed time for it? I guess "rand" is useless now if srand works fine.So srand would always use elapsed time by default and you dont have to write the function in script yourself to use elapsed time for srandom.
I know its hackaround but is there any other way to get random value besides elapsed time? if not then why not making it default instead of having to do this  int time = openborvariant("elapsed_time");
   
    srand(time);.
And then just let us use "rand" instead of "srand".
OR is it exactly the same as "rand" and you can change the seed yourself as extra function ?
 
I know its hackaround but is there any other way to get random value besides elapsed time? if not then why not making it default instead of having to do this  int time = openborvariant("elapsed_time");

I think its what he did on the new version 4079 http://www.chronocrash.com/forum/index.php?topic=1782.0

Could you modify the code so when you're calling "rand" in old mods then youre acutally calling new "srand" and it works fine without having to setup elapsed time for it? I guess "rand" is useless now if srand works fine.So srand would always use elapsed time by default and you dont have to write the function in script yourself to use elapsed time for srandom.
I think this can break stuff beyond the fix.

I still can't download both version, it leads me to the download page main link
 
bWWd said:

I think both you guys missed the whole point of why I added srand(). The random problem is pretty much the opposite of what you think. Our issue is that elapsed time is NOT being used as OpenBOR's default random seed. I was wrong when I said it was earlier.

Turns out the default seed is a sequential counter and that's why "random" numbers appear to be pattern based - they are! See this post and read carefully.

Elapsed time is a perfect seed and IMO it's what should have always been used. That's why I couldn't believe random wasn't working and what got me to take a closer look. Problem is I'm not comfortable messing with a universal default behavior that's been around since the original BOR.

Soooo, what the new srand({seed}) does is give you as an author the option to make OpenBOR to use whatever seed you feel like. In practice, just feed it elapsed time; it's the best and really only way to get a true random value.

DC
 
So did you fixed rand as well or left it with this bug/typo as is ?
People will keep complaing about it till eternity if you will leave it as is.
 
No, I did not "fix" rand() because it isn't broken. It works exactly as designed and though I don't agree with said design I'm not going to change it.

People have been given an alternate in srand() that will do what they want. If they're not going to pay attention, then they can just complain.

DC
 
O Ilusionista said:
If they're not going to pay attention, then they can just complain.
DC, that was quite uncalled-for.

I'm not trying to be a jerk, but my options are limited. You have to remember the random system affects everything. If I change default behavior, then out come the pitchforks and torches. So I did the next best thing and provided a new option. If authors can't be bothered to use it, there's nothing more I can do.

DC
 
can you at least release version with default random changed so we could test in practice if its really breaking things ?
 
Status
Not open for further replies.
Back
Top Bottom