[0.11.x] Minor Random number issue

This subforum contains all the issues which we already resolved.
Post Reply
User avatar
bobingabout
Smart Inserter
Smart Inserter
Posts: 7329
Joined: Fri May 09, 2014 1:01 pm
Contact:

[0.11.x] Minor Random number issue

Post by bobingabout »

This is specifically related to the loot table of entities, it may effect other things too. I discovered this while modding.

Code: Select all

count_min = 0,
count_max = 1,
probability = 1
in this instance you'd expect one item to drop 50% of the time.
The result is that nothing drops, ever.

Code: Select all

count_min = 0,
count_max = 2,
probability = 1
in this case, you would expect around a third of the time nothing to drop, another third only 1, and the final third of the time, 2.
The result is that 50% of the time, nothing drops, the other 50% only 1 drops.


this is obviously wrong.

Theory: the random number generator is based on a floating point, where the literal values are between count_min and count_max. the problem is that you can't drop a fraction of an item, so in the case of 0 to 1, you almost never get an actual 0, or actual 1, there are thousands of possible results, so almost all the time it will land somewhere between the 2 points, meaning you'll get 0.99999 on the random generator, and still get 0 items drop.



I See 2 possible solutions:
1. replace the random number generator in such an instance with an integer random number generator, this could be done with a very simple function.

Code: Select all

return (rand() % max_count) + min_count
Obviously this has no protection in it, but will get the job done with a near enough even ratio throughout the range.
Another possible peice of code if you don't already have one is this extract from a peice of my own code

Code: Select all

inline int random(int min, int max)
{
	return (int)((((long)(max + 1 - min) * rand()) / ((long)RAND_MAX + 1)) + min);
}
This is guaranteed to give as even a result table as possible.

2. Use a round up/down function, so below 0.5 rounds down, and above 0.5 rounds up. in the case of min 0 and max 1, 0 to 0.5 will result in a 0, 0.5 to 1 will result in a 1. however in the case of 0 to 2, 0-0.5 and 1.5 to 2 will result in 0 and 2 respectively, but 0.5 to 1.5 will result in a 1. the chance of getting a 1 is twice as much as the chance to get a 0 or a 2, so not very ballanced.
Creator of Bob's mods. Expanding your gameplay since version 0.9.8.
I also have a Patreon.

kovarex
Factorio Staff
Factorio Staff
Posts: 8053
Joined: Wed Feb 06, 2013 12:00 am
Contact:

Re: [0.11.x] Minor Random number issue

Post by kovarex »

Thx for the report, I fixed the bug for 0.11.20

User avatar
bobingabout
Smart Inserter
Smart Inserter
Posts: 7329
Joined: Fri May 09, 2014 1:01 pm
Contact:

Re: [0.11.x] Minor Random number issue

Post by bobingabout »

Thanks.
my "simple" function was wrong anyway, so I hope you didn't use it :D it should be similar to the bigger function. "max - min +1" gives the range, then "+min" gives the starting point. like so:

Code: Select all

return (rand() % (max_count + 1 - min_count)) + min_count
Creator of Bob's mods. Expanding your gameplay since version 0.9.8.
I also have a Patreon.

Post Reply

Return to “Resolved Problems and Bugs”