change the position of letters
#1

Hi, is it possible to randomize the positions of the letters of a word? I mean like let's say you get a word "apple" then it changes the positions of the letters and one time it will look something like "plepa" and other time "eaplp" etc. I want it to be totally random so players could unscramble these words. I don't want to make it like that "apple" is always "eaplp" It needs to be different every time. Is that possible?
Reply
#2

PHP код:
   new swap,index,i;
   new 
str[]="Hello";
   while(
str[i]!='\0'
    { 
        
swap=str[i]; 
        
index=random(sizeof(str)-1); 
        
str[i] = str[index]; 
        
str[index]=swap
        
i++; 
    }
    
printf(str); 
Reply
#3

Thank you, it works, but sizeof(str) needs to be changed to strlen(str).
Reply
#4

Quote:
Originally Posted by GoldenLion
Посмотреть сообщение
Thank you, it works, but sizeof(str) needs to be changed to strlen(str).
Not in this case as I assigned the string leaving the compiler to determining the cell required so we can avoid an overhead of strlen replacing with a constant which is better optmised.
Reply
#5

Quote:
Originally Posted by Sreyas
Посмотреть сообщение
Not in this case as I assigned the string leaving the compiler to determining the cell required so we can avoid an overhead of strlen replacing with a constant which is better optmised.
Ah, alright. Well I did that in a command and used the params with that so it gave me an error. :P
Reply
#6

Quote:
Originally Posted by GoldenLion
Посмотреть сообщение
Ah, alright. Well I did that in a command and used the params with that so it gave me an error. :P
yeahhh my bad i forgot that it will give error in commonly using compiler (not in modern) as the array size is unknown due as checking is done before calculation.
Reply
#7

pawn Код:
ScrambleString(string[])
{
    for(new i = 0, j = strlen(string), k = random(j); i < j; i ++, k = random(j))
    {
        i != k && (string[i] ^= string[k], string[k] ^= string[i], string[i] ^= string[k]);
    }
    return string;
}
Reply
#8

Quote:
Originally Posted by SickAttack
Посмотреть сообщение
pawn Код:
ScrambleString(string[])
{
    for(new i = 0, j = strlen(string), k = random(j); i < j; i ++, k = random(j))
    {
        i != k && (string[i] ^= string[k], string[k] ^= string[i], string[i] ^= string[k]);
    }
    return string;
}
please explain how does it work?
Reply
#9

Quote:
Originally Posted by admantis
Посмотреть сообщение
please explain how does it work?
https://en.wikipedia.org/wiki/Fisher...3Yates_shuffle

Quote:
Originally Posted by GoldenLion
Посмотреть сообщение
Is that better than Sreyas' one?
It is the modern and "inside-out" versions and to answer to your question: yes, it is.
Reply
#10

Quote:
Originally Posted by Konstantinos
Посмотреть сообщение
https://en.wikipedia.org/wiki/Fisher...3Yates_shuffle



It is the modern and "inside-out" versions and to answer to your question: yes, it is.
Thanks.
Reply


Forum Jump:


Users browsing this thread: 4 Guest(s)