Generate random
#1

Hello, i need a function to generate a 16 digit number with some letters too (like vouchers, so in a pair of 4), example:

12AB-56OA-LE12-NH80

I need it with - too.

Thanks to everyone who will help me.
Reply
#2

Код:
new RandomLetters[5][2]=
{
    "a","b","c","d","e"//..........
}



new code[20];
code[0] = random(RandomLetters[5]);
code[1] = random(9);
This will make first character of the code to random letter (you need to add all the alphabet to letters), and second character to random number from 0 to 9.

Iam not sure is this all OK, so send me errors or problems
Reply
#3

Or you can just not fail and do something similar to this;

pawn Код:
GenerateRandomString()
{
    new str[24];
   
    // 0 = char 48
    // A = char 65
    // a = char 97
   
    for(new i; i < 20; i++) // 20 total chars
    {
        if(i % 4 == 0) // If 'i' is a multiple of 4
        {
            str[i] = '-'; // Insert dash
        }
        else
        {
            switch(random(36)) // 36 cuz 10+26, or 10 digits and 26 alphabet chars
            {
                case 0..9:
                    str[i] = 48 + random(10); // random of 10 digits
                default:
                    str[i] = 65 + random(26); // 65 (char 'A') + random(26), cuz 26 letters in alphabet
            }
        }
    }
   
    return str;
}


@TokicMajstor: Please don't go around "helping" if you don't know how... Generally your answer is okay, but knowing that it probably won't work and saying "come back if you have errors" isn't going to help anyone. This is the section new users are supposed to learn from, not a place for inexperienced users to post things that aren't going to teach someone anything. FOR EXAMPLES; "random(9)" is only going to be 0-8, excluding number 9. "random(RandomLetters[5]);" is going to return an error because "RandomLetters[5]" doesn't represent an integer (which is the only parameter the random function takes), instead you are going to get an out of range error and a confused compiler. The ONLY thing you got right was the number 20 for the "code" array size, and even that is wrong because a null character is needed (requiring the size to be 21).
Reply
#4

Quote:
Originally Posted by SymonClash
Посмотреть сообщение
Thank you both.

Crayder, what if i have to use your GenerateRandomString in a command? How?

Something like this maybe?

pawn Код:
CMD:generate(playerid)
{
    new rand = GenerateRandomString();
    SCMEX(playerid, -1, "Generated code: %d", rand);
    return 1;
}
pawn Код:
CMD:generate(playerid)
{
    SCMEX(playerid, -1, "Generated code: %s", GenerateRandomString());
    return 1;
}
The function returns a string, so format a string not an integer, plus all the stuff is made *inside* the function, no need to random anything just use the function plainly.
Reply
#5

Yeah just tried now. Problem is, the generated code is in this way:

-EKS-8TV-0D6-KCA-WMV

It's possible to remove the - at the start of the code?

I need it like this:

EKS-8TV-0D6-KCA-WMV
Reply
#6

simply change
pawn Код:
if(i % 4 == 0) // If 'i' is a multiple of 4
to
pawn Код:
if(i % 4 == 0 && i != 0) // If 'i' is a multiple of 4 and 'i' is not 0
Reply
#7

lol

This is essentially correct...
Quote:
Originally Posted by bgedition
Посмотреть сообщение
simply change
pawn Код:
if(i % 4 == 0) // If 'i' is a multiple of 4
to
pawn Код:
if(i % 4 == 0 && i != 0) // If 'i' is a multiple of 4 and 'i' is not 0
However, since the dash makes each group actually FIVE chars, the code should be:
pawn Код:
if(i % 5 == 0 && i != 0) // If 'i' is a multiple of 5 and 'i' is not 0
Reply
#8

but the seperator isn't on 5,10,15 it is on index 4,9,14, so you should do
pawn Код:
if(i % 5 == 4)
also I am unsure why you used a switch instead of an if
pawn Код:
new rand = random(36);
// something like that
str[i] = rand + ((rand < 10) ? (48) : (55));
Reply
#9

Quote:
Originally Posted by Nero_3D
Посмотреть сообщение
but the seperator isn't on 5,10,15 it is on index 4,9,14, so you should do
pawn Код:
if(i % 5 == 4)
also I am unsure why you used a switch instead of an if
pawn Код:
new rand = random(36);
// something like that
str[i] = rand + ((rand < 10) ? (48) : (55));
I was just trying to make it easy for him to understand the code honestly, in a way that I could type it on my work tablet which wasn't too terrible, but good points xD
Reply


Forum Jump:


Users browsing this thread: 2 Guest(s)