amxmodx icon indicating copy to clipboard operation
amxmodx copied to clipboard

Feature Request: Better Random Number Generators

Open PartialCloning opened this issue 7 years ago • 14 comments
trafficstars

The current random number generators are not that random.

  1. random.
  2. random_num (Using the engine's RandomLong).

I have no data to back that claim but I'm not the only person to notice a pattern. A forum search shows many posts making the same claim.

In 2005, "Twilight Suzuka" released a better random number generator for AMXMODX, Module: RandomX using the Mersenne Twister PRNG.

RandomX uses a random number generator called the "Mersenne Twister", which is one of the fastest random number generators availible, and is light years ahead of the current ones implimented in AMXx. This means high quality and inexpensive randomized variables.

In 2009 "altex" reported a bug in the random number generator used in Sourcemod (Bug #3831). Mersenne Twister PRNG based random number generators GetURandomInt and GetURandomFloat were then added to SourceMod. Commit: Added better random number generator.

Possible Solutions:

  • Adding RandomX to 1.9.0.
  • Porting SourceMod's implementation into AMXx.

PartialCloning avatar Sep 03 '18 00:09 PartialCloning

Tested Mersenne Twister (std::mt19937)

INT = -13, 0, 13, -7, 10, 6, -9, -14, 9, -11, -8, -5, 0, 7, 10, 10 REAL = -9.73, 10.94, -15.11, -14.70, -13.85, -3.49, -5.18, 2.96, -9.95, 4.09, 13.67, 3.87, -6.42, -6.23, -7.89, -6.28

So Mersenne Twister isn't much more better in my opinion.

ClaudiuHKS avatar Sep 03 '18 13:09 ClaudiuHKS

Repeating numbers aren't a sign of a bad algorithm, that's not how random number generators are tested.

rsKliPPy avatar Sep 03 '18 13:09 rsKliPPy

I'm not searching for some algorithm that isn't repeating numbers.

I know that M.T. method is better but the differences aren't huge though.

Besides, I would definetely agree implementing M.T. into AMXX.

ClaudiuHKS avatar Sep 03 '18 15:09 ClaudiuHKS

random_num(0, 255): random_num(0, 255) random_num(0, 1): random_num(0, 1) random(256): random(256) random(2): random(2)

WPMGPRoSToTeMa avatar Sep 04 '18 01:09 WPMGPRoSToTeMa

You can try what HamletEagle suggested to try the RandomX module https://forums.alliedmods.net/showpost.php?p=2613505&postcount=11

justgo97 avatar Sep 04 '18 11:09 justgo97

AMXX can also add SM's M.T. generator but I strongly recommend that random, random_num and random_float functions will be the same as they actualy are.

ClaudiuHKS avatar Sep 05 '18 17:09 ClaudiuHKS

@ClaudiuHKS as you can see from pictures random isn't so good.

WPMGPRoSToTeMa avatar Sep 05 '18 22:09 WPMGPRoSToTeMa

Oh my.. I've always thought that..

#define random( x ) random_num ( 0, x )

You're right..

ClaudiuHKS avatar Sep 05 '18 22:09 ClaudiuHKS

It's probably best to just add a new function for a CSPRNG

IgnacioFDM avatar Sep 06 '18 00:09 IgnacioFDM

@ClaudiuHKS

#define random( x ) random_num ( 0, x )

#define random( x ) random_num ( 0, ( x ) - 1 ) of course.

WPMGPRoSToTeMa avatar Sep 09 '18 23:09 WPMGPRoSToTeMa

@WPMGPRoSToTeMa can you post a picture showing how different RandomX's or SourceMod's PRNG looks? Do you think such a feature is needed or is random_num good enough?

PartialCloning avatar Sep 11 '18 17:09 PartialCloning

@PartialCloning I think picture is only a minimal test, we need to make additional tests.

WPMGPRoSToTeMa avatar Sep 11 '18 17:09 WPMGPRoSToTeMa

If you do something like this (just an example): public void OnMapStart() { for(int i = 1; i <= 3; fake++) { LogMessage("%i", GetRandomInt(0, 148)); } } and just keep restarting server, it will almost always log the same 3 numbers. (I've tested this only on one server). EDIT: sorry, now I see it's amx topic.

toorisrael avatar Nov 27 '21 07:11 toorisrael

Will this ever be addressed?

Maxi605 avatar Sep 29 '23 20:09 Maxi605