Random script giving an unexpectly expected result

16-bit Fighter

Active member
I created an none object meant to load randomly an item among four possible ones. It's very strange for me what happens and I'll give as much as details possible to explain.

The issue is the result is the same when I start for the first time a stage. It changes following the stage but in each stage as its own result. When I endgame to restart the same level, at last I obtain another result: a kind of real random one. But if I quit OpenBor and restart the game, each stage really has its own expected result.
When I test with several random items in a screen, each item has its own expected result as well. If I add another one its result will depend on where it is in the content of the file, not where it is in the screen. By example, if 5 random items gives 42131, a 6th added one will have 4 if I put as the first in the text (while the ex first random item after becoming the 2nd will have 2 and the ex second random item after becoming the 3th will have 1, and so on) but it will have 1 if it is the 5th.

Now here is the script:
Code:
name        powerx3
type        none
lifespan    5
animationscript    data/scripts/player.c
load         powera
load         powerb
load    powerc
load         powerd

anim    idle
@script
    if(frame == 0){
      void self = getlocalvar("self");
    int r = rand()%20+20;

        if(r < 10){
          changeentityproperty(self, "animation", openborconstant("ANI_FOLLOW1"));
        } else if(r < 20){
          changeentityproperty(self, "animation", openborconstant("ANI_FOLLOW2"));
        } else if(r < 30){
          changeentityproperty(self, "animation", openborconstant("ANI_FOLLOW3"));
        } else if(r < 40){
          changeentityproperty(self, "animation", openborconstant("ANI_FOLLOW4"));
        }
    }
@end_script
    delay    1
    offset    8 14
    frame    data/chars/misc/empty.gif
    frame    data/chars/misc/empty.gif
    delay    250
    frame    data/chars/misc/empty.gif
    delay    250
    frame    data/chars/misc/empty.gif

anim    follow1
    delay    1
    offset    8 14
    frame    data/chars/misc/empty.gif
    delay    200
    @cmd    spawn01 "powera" 0 0 0
    frame    data/chars/misc/empty.gif

anim    follow2
    delay    1
    offset    8 14
    frame    data/chars/misc/empty.gif
    delay    200
    @cmd    spawn01 "powerb" 0 0 0
    frame    data/chars/misc/empty.gif
 
anim    follow3
    delay    1
    offset    8 14
    frame    data/chars/misc/empty.gif
    delay    200
    @cmd    spawn01 "powerc" 0 0 0
    frame    data/chars/misc/empty.gif
 
anim    follow4
    delay    1
    offset    8 14
    frame    data/chars/misc/empty.gif
    delay    200
    @cmd    spawn01 "powerd" 0 0 0
    frame    data/chars/misc/empty.gif

Anyone has an idea what's wrong please? I mean I obviously want a random effect from the start. ^^
 
Last edited:
When you start the first stage, modify the random number generator seed with:

srand(seed)

You can use the "ticket" counter as the seed; it's not ideal, but it usually works fine.
Code:
srand(openborvariant("ticks"));

There are more complex and better ways to generate a seed so that the result "feels" more random, but in general, the human variation between starting the engine and you starting to play usually has a variation that works.

In version 4-7533, without needing to modify the seed, I get random numbers. I couldn't tell you the root of the problem, but I understand that by default, OpenBOR doesn't always use the same seed, so perhaps somewhere you're initializing the seed with the same value?
 
Thanks @hechelion !
Sorry, my level of scripting isn't good enough to understand well what you wrote and I don't know how to but the lines you suggested. I put in a header 'srand(seed)' in the level 1 but nothing changed. I'm still stuck, then.
 
No, that's the instruction, where "seed" is the number of the seed you want to use.

The statement code you should use is:
Code:
srand(openborvariant("ticks"));

On the first level of your game, and only on the first level, check if you have a script that loads with "levelscript".

If you do, edit that script and add the line I indicated.

---------------------------
If you don't have a "levelscript" on your first level, then add this line to your first level's text file:

levelscript data/scripts/oncreatefirstlevel.c

In the "scripts" folder, create the file "oncreatefirstlevel.c" and write the following:
C:
void main(){
    srand(openborvariant("ticks"));
}

-------------------------
The explanation is that in computing, truly random numbers don't exist. Instead, a sequence of numbers generated by an algorithm is used. The numbers jump from one value to another that is always fixed, but this sequence is very, very long and complex.

If you use a part of that sequence and reduce the value of the number, what you get are values that behave as if they were random.

What is usually done, then, is to modify the starting point of your algorithm. This is done by changing the "seed." There are different ways to do this, but a simple one that usually gives decent results is to use the application's startup time. That way, each time the game starts, the seed value will be slightly different, but that small difference will make the sequence of numbers you see with "rand()%20+20" look and feel like it's random.

I don't know which version of OpenBOR you're using. I'm working with the latest version, and without needing to initialize the seed myself, I'm always getting random numbers. That's why I think somewhere in your game you must have a line that initializes the seed, but you're always doing it with the same value, and that's why you're always seeing the same sequence.
 
Back
Top Bottom