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.
Is the random function really random?
Moderator: OpenTTD Developers
Re: Is the random function really random?
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?
Re: Is the random function really random?
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
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
Re: Is the random function really random?
Hm, I guess you can't, and I guess that's a good thing.
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.
You can try something like this to get a random number:In MP we cannot use Random because that will cause desyncs
Code: Select all
local sysdate = GSDate.GetSystemTime() % 200;
this.Sleep(sysdate);
GSLog.Info( GSBase.RandRangeItem( sysdate, 1024 ) );
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.
Re: Is the random function really random?
How exactly do you test?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
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.
Re: Is the random function really random?
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.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.
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
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.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 help, both of you. Much appreciated.
Who is online
Users browsing this forum: Google Adsense [Bot] and 7 guests