Is the random function really random?

Discuss the new AI features ("NoAI") introduced into OpenTTD 0.7, allowing you to implement custom AIs, and the new Game Scripts available in OpenTTD 1.2 and higher.

Moderator: OpenTTD Developers

Post Reply
funkyL
Engineer
Engineer
Posts: 3
Joined: 11 Jul 2016 22:35

Is the random function really random?

Post by funkyL »

Hello all,

I am making a game script that will help me in the process of creating new maps, so I won't have to do more manual placement of industries than necessary. While placing industries I want it to be sort of a random location. But while testing on the same map over and over, I could see that the functions for random in the API always returns the same numbers in the same order for a given map. So if I am unhappy with the placement of the industries and run the script again on the same map, then I don't get a different result. It is all the same locations.

I was thinking about making an option in the settings to make it possible to skip x iterations of the random call before using the result. Thus getting different result every time I change the value in the settings.

So I was wondering how you get around making something random when creating a GS/AI that needs something random? Hope there is a better solution out there than what I am thinking of doing.
User avatar
Sylf
President
President
Posts: 957
Joined: 23 Nov 2010 21:25
Location: ::1

Re: Is the random function really random?

Post by Sylf »

Are you using rand() function to get the random number? Have you tried calling something like srand(floor(clock())) before calling rand(), so it's properly seeded?
funkyL
Engineer
Engineer
Posts: 3
Joined: 11 Jul 2016 22:35

Re: Is the random function really random?

Post by funkyL »

I tried all the random functions from the API. Basicly using GSBase.RandRange() to get random number.

Didn't try the functions you mention, and I can't figure out how to call it from the Gamescript. Do I have to do something special to make use of this function. This probably could solve my problem, if I get something based on the clock :)
User avatar
Sylf
President
President
Posts: 957
Joined: 23 Nov 2010 21:25
Location: ::1

Re: Is the random function really random?

Post by Sylf »

Hm, I guess you can't, and I guess that's a good thing.
In MP we cannot use Random because that will cause desyncs
You can try something like this to get a random number:

Code: Select all

    local sysdate = GSDate.GetSystemTime() % 200;
    this.Sleep(sysdate);
    GSLog.Info( GSBase.RandRangeItem( sysdate, 1024 ) );
Because GetSystemTime() will return the integer representation of the system time, it'll most likely be different each time you run the script.
So, the script will sleep this pseudo random number of game ticks before it gets the random number.
The second use of sysdate variable is not needed - it's just a random integer that I could use in RandRangeItem() function.
After sleeping random number of ticks, you get different random number each time you run the script.
Alberth
OpenTTD Developer
OpenTTD Developer
Posts: 4763
Joined: 09 Sep 2007 05:03
Location: home

Re: Is the random function really random?

Post by Alberth »

But while testing on the same map over and over, I could see that the functions for random in the API always returns the same numbers in the same order for a given map
How exactly do you test?
Do you generate a new map each time, or do you load a savegame?

In the latter case, yeah, it's deterministic, since everything is saved in the savegame, including random number generator state.
Being a retired OpenTTD developer does not mean I know what I am doing.
funkyL
Engineer
Engineer
Posts: 3
Joined: 11 Jul 2016 22:35

Re: Is the random function really random?

Post by funkyL »

Alberth wrote: How exactly do you test?
Do you generate a new map each time, or do you load a savegame?

In the latter case, yeah, it's deterministic, since everything is saved in the savegame, including random number generator state.
I have a map i generated in the scenario editor. It is just a simple map with only 1 town, to make it playable. I saved this as a scenario. Then I will go to 'Play Scenario' for testing, while loading the scenario with my gamescript active. While I'm playing my new scenario, the script is running my test, or what else I would have set it up to. when done testing, I will quit without saving game. I'll load the map in the same way again, after making changes to my script.
This probably means that the game is starting at the same point in the random number generator every time, just as you said. Thanks for clearing this out for me. I didn't know about the game saving the random number state. I thought it was just me using the random functions incorrectly :)
Sylf wrote: You can try something like this to get a random number:

Code: Select all

    local sysdate = GSDate.GetSystemTime() % 200;
    this.Sleep(sysdate);
    GSLog.Info( GSBase.RandRangeItem( sysdate, 1024 ) );
Thanks for the tip.. hadn't thought about using the time for getting a different number each time. I can make some work around from here.

Thanks for the help, both of you. Much appreciated.
Post Reply

Return to “OpenTTD AIs and Game Scripts”

Who is online

Users browsing this forum: No registered users and 5 guests