Get random string
#1

Hey all.
I use random(max) to get a random number

is there any way to get a random string ?
Reply
#2

I dont remember properly but you can do it using ASCII <something> and using random.
Reply
#3

PHP код:
stock minrandom(minmax)
    return 
min random(max min); 
PHP код:
new randomString[10];
for(new 
isizeof randomStringi++)
{
    
randomString[i] = minrandom('A''Z');

If you also want lowercase letters and numbers then you can use another call to random to randomize that, too.
Reply
#4

pawn Код:
GenString( string[ ] , size = sizeof string )
{
    static const Data[ ] = "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ";//add more characters if they want to include in string
    new i;
    for(i = 0 ; i < size; ++i)
        string[ i ] = Data[ random( sizeof Data ) ];
}
pawn Код:
new string[10];
GenString(string);
printf("random string : %s ",string);
Reply
#5

Quote:
Originally Posted by SyS
Посмотреть сообщение
pawn Код:
GenString( string[ ] , size = sizeof string )
{
    static const Data[ ] = "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ";//add more characters if they want to include in string
    new i;
    for(i = 0 ; i < size; ++i)
        string[ i ] = Data[ random( sizeof Data ) ];
}
pawn Код:
new string[10];
GenString(string);
printf("random string : %s ",string);
I tryied this code like this...but pawno give me 2 warning, who told me about the functions that should have a return value, how I fix that?
pawn Код:
CMD:provahash(playerid, params[])
{
    new str[256];
    new strfull[10], strnf[10];
    format(str, sizeof(str), "{FFFFFF}Full: {FFFF00}%s", RandomStringFull(strfull));
    SendClientMessage(playerid, -1, str);
    format(str, sizeof(str), "{FFFFFF}no full: {FFFF00}%s", RandomString(strnf));
    SendClientMessage(playerid, -1, str);
    format(str, sizeof(str), "{FFFFFF}numbers: {FFFF00}%d", random(999999999));
    SendClientMessage(playerid, -1, str);
    return 1;
}

RandomString( string[ ] , size = sizeof string )
{
    static const Data[ ] = "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ";//add more characters if they want to include in string
    new i;
    for(i = 0 ; i < size; ++i)
        string[ i ] = Data[ random( sizeof Data ) ];
}

RandomStringFull( string[ ] , size = sizeof string )
{
    static const Data[ ] = "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ!Ј$%&/()=?'^мий+*тза°щ§,;.:-_<>";//add more characters if they want to include in string
    new i;
    for(i = 0 ; i < size; ++i)
        string[ i ] = Data[ random( sizeof Data ) ];
}
Reply
#6

Quote:
Originally Posted by OuDayas
Посмотреть сообщение
I tryied this code like this...but pawno give me 2 warning, who told me about the functions that should have a return value, how I fix that?
pawn Код:
CMD:provahash(playerid, params[])
{
    new str[256];
    new strfull[10], strnf[10];
    format(str, sizeof(str), "{FFFFFF}Full: {FFFF00}%s", RandomStringFull(strfull));
    SendClientMessage(playerid, -1, str);
    format(str, sizeof(str), "{FFFFFF}no full: {FFFF00}%s", RandomString(strnf));
    SendClientMessage(playerid, -1, str);
    format(str, sizeof(str), "{FFFFFF}numbers: {FFFF00}%d", random(999999999));
    SendClientMessage(playerid, -1, str);
    return 1;
}

RandomString( string[ ] , size = sizeof string )
{
    static const Data[ ] = "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ";//add more characters if they want to include in string
    new i;
    for(i = 0 ; i < size; ++i)
        string[ i ] = Data[ random( sizeof Data ) ];
}

RandomStringFull( string[ ] , size = sizeof string )
{
    static const Data[ ] = "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ!Ј$%&/()=?'^мий+*тза°щ§,;.:-_<>";//add more characters if they want to include in string
    new i;
    for(i = 0 ; i < size; ++i)
        string[ i ] = Data[ random( sizeof Data ) ];
}
Those functions do not return anything, but you put them into the format line. That's why the compiler complains.

You should move the functions out of the format call, since they modify the arguments that get passed to them (by reference).

Use them like this:

Код:
new string[50];
RandomString(string); // string will now contain the random string
Reply
#7

Use RyDeR function:
https://forum.sa-mp.com/showpost.php...55&postcount=5
Reply


Forum Jump:


Users browsing this thread: 1 Guest(s)