SA-MP Forums Archive
How to invert randomly a string? - Printable Version

+- SA-MP Forums Archive (https://sampforum.blast.hk)
+-- Forum: SA-MP Scripting and Plugins (https://sampforum.blast.hk/forumdisplay.php?fid=8)
+--- Forum: Scripting Help (https://sampforum.blast.hk/forumdisplay.php?fid=12)
+--- Thread: How to invert randomly a string? (/showthread.php?tid=535186)



How to invert randomly a string? - Hantex. - 02.09.2014

Hi guys, i want to invert a string randomly, example: "hello" - "elhlo" (this totally random), How can i do?

(Sorry for my bad English)


Re: How to invert randomly a string? - Pottus - 02.09.2014

This would actually be quite similar to a card shuffle algorithm actually why not look a few up and see if you can get it to work.


AW: Re: How to invert randomly a string? - Hantex. - 02.09.2014

Quote:
Originally Posted by Pottus
View Post
This would actually be quite similar to a card shuffle algorithm actually why not look a few up and see if you can get it to work.
I tried but I can't do it randomly.


Re: How to invert randomly a string? - Dignity - 02.09.2014

Quote:
Originally Posted by Pottus
View Post
This would actually be quite similar to a card shuffle algorithm actually why not look a few up and see if you can get it to work.
Why didn't you just link him your RandomizeWord function?

Here it is, it does exactly what you want:

pawn Code:
RandomizeWord(word[])
{
    new tmp;
    new r;

    for(new i = strlen(word) - 1; i >= 0; i--)
    {
        r=random(strlen(word));
        tmp = word[i];
        word[i] = word[r];
        word[r] = tmp;
    }
}
Example code:

pawn Code:
#include <a_samp>

public OnFilterScriptInit()
{
    new word[9];
   
    word = "Computer";
    RandomizeWord(word);

    printf("%s", word);

    return true;
}

RandomizeWord(word[])
{
    new tmp;
    new r;

    for(new i = strlen(word) - 1; i >= 0; i--)
    {
        r=random(strlen(word));
        tmp = word[i];
        word[i] = word[r];
        word[r] = tmp;
    }
}
Output:

Code:
[16:11:41]   Loading filterscript 'comp.amx'...
[16:11:41] Cpumeotr
Sauce: http://forum.sa-mp.com/showpost.php?...68&postcount=3


AW: How to invert randomly a string? - Hantex. - 02.09.2014

@Mionee: Thank you, very much.