How to invert randomly a string?
#1

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

(Sorry for my bad English)
Reply
#2

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.
Reply
#3

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.
Reply
#4

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
Reply
#5

@Mionee: Thank you, very much.
Reply


Forum Jump:


Users browsing this thread: 3 Guest(s)