Zandronum Chat on our Discord Server Get the latest version: 3.2
Source Code

View Revisions: Issue #1698 Back to Issue ]
Summary 0001698: botscript rand() is just plain wrong
Revision 2014-02-09 02:06 by Dusk
Description

    g_iReturnInt = g_RandomBotCmdSeed.Random( ) % lMax;
    while ( g_iReturnInt < lMin )
        g_iReturnInt += ( lMax - lMin );


first of all, this causes lMax not to be a possible return value. this is in direct clash with ACS's random() which is confusing.

secondly, this causes bias. if suppose rand is called with, say args 5 and 9, this would first get seed.Random() % 9, which gets a random value between 0 and 8 inclusive.

the while loop then offsets the values. possible outcomes:
0 (less than 5): -> 4 -> 8
1 (less than 5): -> 5
2 (less than 5): -> 6
3 (less than 5): -> 7
4 (less than 5): -> 8
5
6
7
8

overall probability:
- 5: 2 / 9 = 22.2%
- 6: 2 / 9 = 22.2%
- 7: 2 / 9 = 22.2%
- 8: 3 / 9 = 33.3%
- 9: 0 / 9 = 0%

notice how it favors the value 8. suggested change:

    g_iReturnInt = g_RandomBotCmdSeed.Random( ) % ( lMax - lMin + 1 ) + lMin;
Revision 2014-02-09 02:07 by Dusk
Description botcommands.cpp:867:

    g_iReturnInt = g_RandomBotCmdSeed.Random( ) % lMax;
    while ( g_iReturnInt < lMin )
        g_iReturnInt += ( lMax - lMin );


first of all, this causes lMax not to be a possible return value. this is in direct clash with ACS's random() which is confusing.

secondly, this causes bias. if suppose rand is called with, say args 5 and 9, this would first get seed.Random() % 9, which gets a random value between 0 and 8 inclusive.

the while loop then offsets the values. possible outcomes:
0 (less than 5): -> 4 -> 8
1 (less than 5): -> 5
2 (less than 5): -> 6
3 (less than 5): -> 7
4 (less than 5): -> 8
5
6
7
8

overall probability:
- 5: 2 / 9 = 22.2%
- 6: 2 / 9 = 22.2%
- 7: 2 / 9 = 22.2%
- 8: 3 / 9 = 33.3%
- 9: 0 / 9 = 0%

notice how it favors the value 8. suggested change:

    g_iReturnInt = g_RandomBotCmdSeed.Random( ) % ( lMax - lMin + 1 ) + lMin;
Revision 2014-02-09 02:06 by Dusk
Additional Information
Revision 2014-02-09 02:10 by Dusk
Additional Information If we absolutely have to keep the original behavior for backwards compatibility, I suggest we introduce a new random() for this behavior. I can make botc use this one for random() and not use the current, broken rand().
Revision 2014-02-09 02:11 by Dusk
Additional Information if we absolutely have to keep the original behavior for backwards compatibility, I suggest we introduce a new random() for this behavior. I can make botc use this one for random() and not use the current, broken rand().






Questions or other issues? Contact Us.

Links


Copyright © 2000 - 2025 MantisBT Team
Powered by Mantis Bugtracker