Raised This Month: $32 Target: $400
 8% 

Solved random


Post New Thread Reply   
 
Thread Tools Display Modes
Bugsy
AMX Mod X Moderator
Join Date: Feb 2005
Location: NJ, USA
Old 01-21-2018 , 12:49   Re: random
Reply With Quote #11

This is basically what Natsheh suggested in his first post:
PHP Code:

#include <amxmodx>
#include <amxmisc>

const MaxNumber 30;
const 
ExcludeNumbers = ( ( << 13 ) | ( << 22 ) );

new 
g_RandomNumbersMaxNumber ];

public 
plugin_init()
{
    
LoadRandomNumbers();
    
    
//Access your random number with this: g_RandomNumbers[ random( MaxNumber ) ]
    
server_print"Random=%d" g_RandomNumbersrandomMaxNumber ) ] );
}

public 
LoadRandomNumbers()
{
    new 
iNumber iIndex;
    
    while ( ( 
iIndex MaxNumber ) && ( iNumber <= MaxNumber ) )
    {
        if ( !( 
ExcludeNumbers & ( << iNumber ) ) )
        {
            
g_RandomNumbersiIndex++ ] = iNumber;
        }
        
        
iNumber++;
    }

__________________

Last edited by Bugsy; 01-21-2018 at 12:53.
Bugsy is offline
Natsheh
Veteran Member
Join Date: Sep 2012
Old 01-21-2018 , 13:03   Re: random
Reply With Quote #12

Quote:
Originally Posted by Bugsy View Post
This is basically what Natsheh suggested in his first post:
PHP Code:

#include <amxmodx>
#include <amxmisc>

const MaxNumber 30;
const 
ExcludeNumbers = ( ( << 13 ) | ( << 22 ) );

new 
g_RandomNumbersMaxNumber ];

public 
plugin_init()
{
    new 
size LoadRandomNumbers();
    
    
//Access your random number with this: g_RandomNumbers[ random( size) ]
    
server_print"Random=%d" g_RandomNumbersrandomsize ) ] );
}

public 
LoadRandomNumbers()
{
    new 
iNumber iIndexsize;
    
    while ( ( 
iIndex MaxNumber ) && ( iNumber <= MaxNumber ) )
    {
        if ( !( 
ExcludeNumbers & ( << iNumber ) ) )
        {
            
g_RandomNumbersiIndex++ ] = iNumber;
                        
Size++;
        }
                
iNumber++;
    }

        return 
size;

Nope that not quite right i edited the post
__________________
@Jailbreak Main Mod v2.7.0 100%
@User Tag Prefix 100% done !
@Mystery Box 100% done !
@VIP System 100% done !

Natsheh is offline
Send a message via MSN to Natsheh Send a message via Skype™ to Natsheh
Bugsy
AMX Mod X Moderator
Join Date: Feb 2005
Location: NJ, USA
Old 01-21-2018 , 13:09   Re: random
Reply With Quote #13

Quote:
Originally Posted by Natsheh View Post
Nope that not quite right i edited the post
That's great, lol. But the code works fine.
__________________
Bugsy is offline
fysiks
Veteran Member
Join Date: Sep 2007
Location: Flatland, USA
Old 01-21-2018 , 13:33   Re: random
Reply With Quote #14

Quote:
Originally Posted by Black Rose View Post
The chance of that happening in that span with so few exceptions is so small that it's not even worth caring about.
You are correct that the chance of an infinite loop happening is extremely small. However, that's not the problem when it comes to this type of thing. The problem is that the number of loops required is ALWAYS unknown (it could be 1 and it could be 500 or worse). This is called non-deterministic execution. You should always strive for deterministic execution especially because most code executed in plugins is blocking.

All of this was discussed thoroughly in the thread noted by PRoSToTeM@.
__________________
fysiks is online now
Black Rose
Veteran Member
Join Date: Feb 2011
Location: Stockholm, Sweden
Old 01-21-2018 , 20:51   Re: random
Reply With Quote #15

Quote:
Originally Posted by fysiks View Post
You are correct that the chance of an infinite loop happening is extremely small. However, that's not the problem when it comes to this type of thing. The problem is that the number of loops required is ALWAYS unknown (it could be 1 and it could be 500 or worse). This is called non-deterministic execution. You should always strive for deterministic execution especially because most code executed in plugins is blocking.

All of this was discussed thoroughly in the thread noted by PRoSToTeM@.
You don't discuss fysiks, you tell people.

It could be 500, but it won't be. And even if it would be 500, though the chance is 0.0000000000000000000000000000000000000000000 000000000000000000000000000000000000000000000 000000000000000000000000000000000000000000000 000000000000000000000000000000000000000000000 000000000000000000000000000000000000000000000 000000000000000000000000000000000000000000000 000000000000000000000000000000000000000000000 000000000000000000000000000000000000000000000 000000000000000000000000000000000000000000000 000000000000000000000000000000000000000000000 000000000000000000000000000000000000000000000 000000000000000000000000000000000000000000000 000000000000000000000000000000000000000000000 00009% (yes, that is the actual chance), you wouldn't even notice it because 500 cycles of such a small code is done in less than one millisecond.

I get it, it's good to avoid the unknown, but only when it's reasonable to do so.

I assume you agree with my previous example. It would be stupid to cache 1.45TiB memory and do one guaranteed cycle. But the same logic still applies.
Where do you draw the line?
How much is too much to buffer to enter the unknown?
Or is it a question of probability instead of memory consumption because -9*10589 to 1 didn't seem enough.

Another example. Let's say you want 10 random points in a circle from a point in a 2 dimensional world. How would you go about that with your fancy lookup table?

The thing is, you often tell people what to do but you never give any other reason except "it's the right way".
Black Rose is offline
fysiks
Veteran Member
Join Date: Sep 2007
Location: Flatland, USA
Old 01-21-2018 , 23:34   Re: random
Reply With Quote #16

Quote:
Originally Posted by Black Rose View Post
I get it, it's good to avoid the unknown, but only when it's reasonable to do so.
And in this case, it is 100% reasonable to do it the deterministic way. Obviously, the person that writes their own code as final say as to what is actually put in their code. So, they get to evaluate the various methods proposed and choose the one that the want.
__________________

Last edited by fysiks; 01-21-2018 at 23:35.
fysiks is online now
PRoSToTeM@
Veteran Member
Join Date: Jan 2010
Location: Russia, Ivanovo
Old 01-22-2018 , 01:12   Re: random
Reply With Quote #17

Quote:
Originally Posted by Black Rose View Post
Randomizing a temporary filename with 6 characters, assuming we use all lowercase, uppercase and numeric characters and want to avoid an infinite loop we would end up with 26+26+10 different characters ^6, therefor 56 800 235 584 different options. Saving those in an array of 7 cells each since you need to null-terminate that's 1.45Terabyte of memory.
This method should work good here, it requires strong order (maybe weak order can work too), which strings have.
__________________
PRoSToTeM@ is offline
Send a message via ICQ to PRoSToTeM@ Send a message via Skype™ to PRoSToTeM@
indraraj striker
Veteran Member
Join Date: Mar 2014
Location: Under the water
Old 01-22-2018 , 01:45   Re: random
Reply With Quote #18

lol. thanks everyone
#solved
indraraj striker is offline
Black Rose
Veteran Member
Join Date: Feb 2011
Location: Stockholm, Sweden
Old 01-29-2018 , 16:28   Re: random
Reply With Quote #19

I know it proves nothing, just for fun.

Code:
#include <amxmodx> #include <rose> public plugin_init() {     register_plugin("Test Plugin 1", "1.0", "[ --{-@ ]");     return;     new szTimer[16];     new record, totalcycles, totalloops, hTimer = TimerStart();     while ( record < 500 ) {         new i, a;         totalcycles++;         do {             i++;             totalloops++;             a = random_num(0, 30);         }         while (a == 13 || a == 22);         if ( i > record ) {             TimerFormat(TimerMid(hTimer), szTimer, charsmax(szTimer), 1, true);             server_print("%s: New record: %d loops before exiting. (Total cycles: %d, total loops: %d, avg loops: %.2f)", szTimer, i, totalcycles, totalloops, 1.0 * totalloops / totalcycles);             record = i;         }     } } /* 00:00:00:00.000: New record: 2 loops before exiting. (Total cycles: 1, total loops: 2, avg loops: 2.00) 00:00:00:00.000: New record: 3 loops before exiting. (Total cycles: 352, total loops: 375, avg loops: 1.06) 00:00:00:00.001: New record: 4 loops before exiting. (Total cycles: 2017, totalloops: 2183, avg loops: 1.08) 00:00:00:00.005: New record: 5 loops before exiting. (Total cycles: 11173, total loops: 11943, avg loops: 1.06) 00:00:00:00.306: New record: 6 loops before exiting. (Total cycles: 1247442, total loops: 1333647, avg loops: 1.06) 00:00:00:03.651: New record: 7 loops before exiting. (Total cycles: 15484486, total loops: 16552027, avg loops: 1.06) 00:00:01:24.609: New record: 8 loops before exiting. (Total cycles: 360494522, total loops: 385358373, avg loops: 1.06) 00:00:20:56.957: New record: 9 loops before exiting. (Total cycles: 1055510066,total loops: 1424513328, avg loops: 1.34) 00:00:21:59.124: New record: 10 loops before exiting. (Total cycles: outside range, total loops: outside range, avg loops: N/A) 03:19:25:xx.xxx: I was only supposed to run it overnight but forgot it for almost 4 days.  */
Black Rose is offline
PRoSToTeM@
Veteran Member
Join Date: Jan 2010
Location: Russia, Ivanovo
Old 01-30-2018 , 23:11   Re: random
Reply With Quote #20

Now try to use custom random function that always returns 13. xD
__________________
PRoSToTeM@ is offline
Send a message via ICQ to PRoSToTeM@ Send a message via Skype™ to PRoSToTeM@
Reply


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT -4. The time now is 02:54.


Powered by vBulletin®
Copyright ©2000 - 2024, vBulletin Solutions, Inc.
Theme made by Freecode