SA-MP Forums

Go Back   SA-MP Forums > SA-MP Scripting and Plugins > Filterscripts

Reply
 
Thread Tools Display Modes
Old 01/02/2017, 02:19 PM   #3941
SyS
High-roller
 
SyS's Avatar
 
Join Date: Oct 2015
Posts: 1,939
Reputation: 504
Default Re: Useful Functions

Quote:
Originally Posted by Lordzy View Post
Yes it does, but I suppose that could be an issue if the file has been failed to be created. For instance, modifications over file permissions.
Yeah you are right.i didn't think about that situation my mistake
SyS is offline   Reply With Quote
Old 09/02/2017, 07:13 AM   #3942
iLearner
Guest
 
Posts: n/a
Default Re: Useful Functions

Quote:
Originally Posted by Lordzy View Post
ShuffleArray - Function shuffles the items in the given array.

Parameters:
array[] - The array to be shuffled.
start = 0 - The starting point of the array index to start shuffling. (optional)
end = sizeof(array) - The ending point of the array index to be shuffled. (optional)

pawn Code:
stock ShuffleArray(array[], start = 0, end = sizeof(array)) {

    new
        rand_Value,
        i = start,
        arr_Index = start,
        temp_Var
    ;

    while(i < end) {

        rand_Value = random(end - start) + start;
        temp_Var = array[arr_Index];

        array[arr_Index] = array[rand_Value];
        array[rand_Value] = temp_Var;

        arr_Index = rand_Value;

        i++;
    }
    return 1;
}

Example:
pawn Code:
public OnFilterScriptInit() {

    new
        my_Array[10] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10},
        tempStr[25],
        shortStr[5]
    ;
   
    print("Before shuffling, my_Array = ");
    for(new i = 0; i< 10; i++) {
   
        valstr(shortStr, my_Array[i], false);
        strcat(tempStr, shortStr, sizeof(tempStr));
        strcat(tempStr, " ", sizeof(tempStr));
    }
    print(tempStr);
   
    tempStr[0] = '\0';
    print("After shuffling from 0 -> 10 (start = 0 | end = 10)");
    ShuffleArray(my_Array); //default arguments start = 0, end = sizeof(array)

    for(new i = 0; i< 10; i++) {

        valstr(shortStr, my_Array[i], false);
        strcat(tempStr, shortStr, sizeof(tempStr));
        strcat(tempStr, " ", sizeof(tempStr));
    }
    print(tempStr);

    tempStr[0] = '\0';
    print("Default array again : ");
   
    my_Array = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
   
    for(new i = 0; i< 10; i++) {

        valstr(shortStr, my_Array[i], false);
        strcat(tempStr, shortStr, sizeof(tempStr));
        strcat(tempStr, " ", sizeof(tempStr));
    }
    print(tempStr);

    tempStr[0] = '\0';
    print("After shuffling from 5 -> 10 (start = 5 | end = 10)");
    ShuffleArray(my_Array, 5, sizeof(my_Array)); //sizeof(my_Array) here is 10.
   
    for(new i = 0; i< 10; i++) {

        valstr(shortStr, my_Array[i], false);
        strcat(tempStr, shortStr, sizeof(tempStr));
        strcat(tempStr, " ", sizeof(tempStr));
    }
    print(tempStr);

    return 1;
}

Output:
Code:
Before shuffling, my_Array = 
1 2 3 4 5 6 7 8 9 10 
After shuffling from 0 -> 10 (start = 0 | end = 10)
2 10 3 1 5 8 6 9 7 4 
Default array again : 
1 2 3 4 5 6 7 8 9 10 
After shuffling from 5 -> 10 (start = 5 | end = 10)
1 2 3 4 5 7 6 10 8 9
I believe this function can be even more randomized to make it better, I'll work on it when I got some free time and mostly some interest.
Thanks! I was trying to do it myself with a very bad way!
  Reply With Quote
Old 09/02/2017, 01:00 PM   #3943
SyS
High-roller
 
SyS's Avatar
 
Join Date: Oct 2015
Posts: 1,939
Reputation: 504
Default Re: Useful Functions

Shuffle Array
Version 1

Gist Link

This version of shuffle array uses less memory than normal one.
Parameters
  • array - array to be shuffled
  • start - starting index (optional)
  • end - ending index (optional)

PHP Code:
ShuffleArray( array[ ] , start end sizeof( array ) )//by Sreyas
    
for( new rand_index randomend start ) + startstartend; ++rand_index randomend start 1) + start )
        array[ 
] = array[ rand_index ] + array[ ] - ( array[ rand_index ] = array[ ] ); 


Version 2

Gist Link

This version is faster than above all functions and uses less memory.

Parameters
  • array - array to be shuffled
  • start - starting index (optional)
  • end - ending index (optional)
PHP Code:
ShuffleArray( array[ ] , start end sizeof( array ) )//by Sreyas
{
    static  
rand_index difference,temp;
    
start,
    
difference end start 1;
    while(
end)
          
rand_index randomdifference ) + start temp = array[ ],array[ ] = array[ rand_index ],array[ rand_index ] = temp,++i;



Last edited by SyS; 10/02/2017 at 04:06 AM.
SyS is offline   Reply With Quote
Old 09/02/2017, 01:29 PM   #3944
Lordzy
High-roller
 
Lordzy's Avatar
 
Join Date: Mar 2012
Location: NetherRealm
Posts: 2,698
Reputation: 1177
Default Re: Useful Functions

Quote:
Originally Posted by Sreyas View Post
This version of shuffle array is even more faster than normal one.
I'm not sure if you referred to my version but just clarifying that mine is slightly faster than yours. I can still see rooms for that code to be improved (not only the speed) but I lack time these days to have a good thinking. However, yours is simple and short, which is actually nice!
__________________
Currently inactive - I don't play at any SA-MP servers nor work on anything in PAWN for now. The projects that I've done so far in PAWN, which requires updates will be taking some time.
Lordzy is offline   Reply With Quote
Old 09/02/2017, 01:34 PM   #3945
GhostHacker9
Big Clucker
 
GhostHacker9's Avatar
 
Join Date: Jan 2017
Location: New Zealand
Posts: 105
Reputation: 109
Default Re: Useful Functions

Quote:
Originally Posted by Lordzy View Post
I'm not sure if you referred to my version but just clarifying that mine is slightly faster than yours. I can still see rooms for that code to be improved (not only the speed) but I lack time these days to have a good thinking. However, yours is simple and short, which is actually nice!
pawn's calculation speed can vary with machines.But that version (by Sreyas) uses lesser variables so the stack of that function is optimised and needed to be fast as per my calculation.But the version by Slice is 100% slower than both of yours.
GhostHacker9 is offline   Reply With Quote
Old 09/02/2017, 02:45 PM   #3946
SyS
High-roller
 
SyS's Avatar
 
Join Date: Oct 2015
Posts: 1,939
Reputation: 504
Default Re: Useful Functions

Quote:
Originally Posted by GhostHacker9 View Post
pawn's calculation speed can vary with machines.But that version (by Sreyas) uses lesser variables so the stack of that function is optimised and needed to be fast as per my calculation.But the version by Slice is 100% slower than both of yours.
Nope Lordzy was right.Even though its stack was optimised it involved calculation mechanisms in which abstract machine get really slow.But i have made another version of it and is now faster. (Thanks to Lordzy for providing me test code mine was deprecated).

EDIT: I again modified it and now its far more faster.

Last edited by SyS; 09/02/2017 at 03:18 PM.
SyS is offline   Reply With Quote
Old 09/02/2017, 04:27 PM   #3947
Nero_3D
High-roller
 
Nero_3D's Avatar
 
Join Date: Jun 2007
Location: Germany
Posts: 2,856
Reputation: 692
Default Re: Useful Functions

I don't think that static is necessary or yield any speed advantage but my main problem with your code is the algorithm
You do random(difference) + start, that could result in something like that

ShuffleArray({1, 2, 3, 4, 5}, 0, 5)

rand_index_0 = 3 // random(5) + 0
array = {4, 2, 3, 1, 5}
rand_index_1 = 2
array = {4, 3, 2, 1, 5}
rand_index_2 = 1
array = {4, 2, 3, 1, 5}
rand_index_3 = 0
array = {1, 2, 3, 4, 5}
rand_index_4 = 4
array = {1, 2, 3, 4, 5}

Also if you want speed than you should use emit to avoid the recalculation of the addresses and faster swaps
Nero_3D is offline   Reply With Quote
Old 09/02/2017, 05:44 PM   #3948
iLearner
Guest
 
Posts: n/a
Default Re: Useful Functions

How can i shuffle for example this:

PHP Code:
new MyArray[MAX_MESSAGES][7][200] =
{
    {
"hey""Hello, how are you."},
    {
"hi""Hello , how are you."}
}; 
to make the outcome shuffle the messages, for example move first element in second cell (currently i access first element with MyArray[0][...]..., what i wanted to do is, shuffle the messages and change their pos)
  Reply With Quote
Old 10/02/2017, 12:37 AM   #3949
SyS
High-roller
 
SyS's Avatar
 
Join Date: Oct 2015
Posts: 1,939
Reputation: 504
Default Re: Useful Functions

Quote:
Originally Posted by Nero_3D View Post
I don't think that static is necessary or yield any speed advantage but my main problem with your code is the algorithm
You do random(difference) + start, that could result in something like that

ShuffleArray({1, 2, 3, 4, 5}, 0, 5)

rand_index_0 = 3 // random(5) + 0
array = {4, 2, 3, 1, 5}
rand_index_1 = 2
array = {4, 3, 2, 1, 5}
rand_index_2 = 1
array = {4, 2, 3, 1, 5}
rand_index_3 = 0
array = {1, 2, 3, 4, 5}
rand_index_4 = 4
array = {1, 2, 3, 4, 5}

Also if you want speed than you should use emit to avoid the recalculation of the addresses and faster swaps
no the static variable doesn't yield extra speed but function's member variables won't be created repeatedly on each overhead to that function. What I aimed at that was memory managing.

Sorry about differences variable there was +1 too which I accidently deleted after modifying version2

edit: seems using static variable is yielding speed as recreation is not done

Last edited by SyS; 10/02/2017 at 01:45 AM.
SyS is offline   Reply With Quote
Old 10/02/2017, 01:49 PM   #3950
renatog
Huge Clucker
 
Join Date: Mar 2016
Posts: 327
Reputation: 48
Default Re: Useful Functions

Quote:
Originally Posted by Sreyas View Post
no the static variable doesn't yield extra speed but function's member variables won't be created repeatedly on each overhead to that function. What I aimed at that was memory managing.

Sorry about differences variable there was +1 too which I accidently deleted after modifying version2

edit: seems using static variable is yielding speed as recreation is not done
Static local variables are good for functions that are called a lot because the function doesn't need to recreate variables every time (the variables remains after execution). I don't see you saving memory using a local static, nor speed if it isn't called a lot.
Anyway I think it's more optional for who knows what are doing, if I'll use this in a low interval timer, OnPlayerUpdate, with large arrays etc. I'll use 'static', otherwise 'new'.
renatog is offline   Reply With Quote
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


Similar Threads
Thread Thread Starter Forum Replies Last Post
[Include] [INC] LSF - Lorenc's Simple Functions (w/ gang/clan functions) Lorenc_ Includes 14 21/04/2018 04:06 AM
Functions? sciman001 Help Archive 7 19/03/2011 03:03 PM
How to get functions? Twain32 Help Archive 4 08/10/2010 07:37 PM
[Include] [INC] SA:MP New Functions 0.4a DKN ipsBruno Lançamentos/Releases 22 17/06/2010 08:38 AM
What this functions do? harrold Help Archive 2 08/05/2009 11:30 PM


All times are GMT. The time now is 01:09 AM.


Powered by vBulletin® Version 3.8.6
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.