SA-MP Forums

Go Back   SA-MP Forums > SA-MP Scripting and Plugins > Scripting Help

Reply
 
Thread Tools Display Modes
Old 28/03/2019, 08:44 PM   #1
SymonClash
Banned
 
Join Date: Dec 2018
Posts: 257
Reputation: 25
Default Generate random

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.
SymonClash is offline   Reply With Quote
Old 28/03/2019, 11:11 PM   #2
TokicMajstor
Big Clucker
 
TokicMajstor's Avatar
 
Join Date: Oct 2018
Location: Bosnia And Herzegovina
Posts: 177
Reputation: 1
Default Re: Generate random

Code:
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
__________________


If Iam better than you = you're noob
If you're better than me = you're cheater
If everyone is better than me = I have lags
#Logic
TokicMajstor is offline   Reply With Quote
Old 29/03/2019, 03:07 AM   #3
Crayder
Banned
 
Join Date: Sep 2013
Location: Flames of Hell
Posts: 3,370
Reputation: 652
Default Re: Generate random

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

pawn Code:
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).

Last edited by Crayder; 29/03/2019 at 03:42 AM.
Crayder is offline   Reply With Quote
Old 29/03/2019, 08:22 AM   #4
TheToretto
Gangsta
 
TheToretto's Avatar
 
Join Date: Jul 2015
Posts: 596
Reputation: 66
Default Re: Generate random

Quote:
Originally Posted by SymonClash View Post
Thank you both.

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

Something like this maybe?

pawn Code:
CMD:generate(playerid)
{
    new rand = GenerateRandomString();
    SCMEX(playerid, -1, "Generated code: %d", rand);
    return 1;
}
pawn Code:
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.
__________________






Toretto#9902

Rest in peace grandpa, 03 November 2018
TheToretto is offline   Reply With Quote
Old 29/03/2019, 08:24 AM   #5
SymonClash
Banned
 
Join Date: Dec 2018
Posts: 257
Reputation: 25
Default Re: Generate random

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
SymonClash is offline   Reply With Quote
Old 29/03/2019, 04:45 PM   #6
bgedition
Huge Clucker
 
bgedition's Avatar
 
Join Date: Oct 2013
Location: 127.0.0.1
Posts: 422
Reputation: 37
Default Re: Generate random

simply change
pawn Code:
if(i % 4 == 0) // If 'i' is a multiple of 4
to
pawn Code:
if(i % 4 == 0 && i != 0) // If 'i' is a multiple of 4 and 'i' is not 0
__________________
Never point your finger at some one, you will always have three fingers pointing back at you.
bgedition is offline   Reply With Quote
Old 30/03/2019, 06:50 AM   #7
Crayder
Banned
 
Join Date: Sep 2013
Location: Flames of Hell
Posts: 3,370
Reputation: 652
Default Re: Generate random

lol

This is essentially correct...
Quote:
Originally Posted by bgedition View Post
simply change
pawn Code:
if(i % 4 == 0) // If 'i' is a multiple of 4
to
pawn Code:
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 Code:
if(i % 5 == 0 && i != 0) // If 'i' is a multiple of 5 and 'i' is not 0
Crayder is offline   Reply With Quote
Old 30/03/2019, 12:00 PM   #8
Nero_3D
High-roller
 
Nero_3D's Avatar
 
Join Date: Jun 2007
Location: Germany
Posts: 2,813
Reputation: 650
Default Re: Generate random

but the seperator isn't on 5,10,15 it is on index 4,9,14, so you should do
pawn Code:
if(i % 5 == 4)
also I am unsure why you used a switch instead of an if
pawn Code:
new rand = random(36);
// something like that
str[i] = rand + ((rand < 10) ? (48) : (55));
Nero_3D is offline   Reply With Quote
Old 30/03/2019, 04:31 PM   #9
Crayder
Banned
 
Join Date: Sep 2013
Location: Flames of Hell
Posts: 3,370
Reputation: 652
Default Re: Generate random

Quote:
Originally Posted by Nero_3D View Post
but the seperator isn't on 5,10,15 it is on index 4,9,14, so you should do
pawn Code:
if(i % 5 == 4)
also I am unsure why you used a switch instead of an if
pawn Code:
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
Crayder is offline   Reply With Quote
Reply

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Generate random characters ServerFiles Scripting Help 1 30/05/2018 12:23 PM
generate random Bussyman Scripting Help 1 15/04/2017 10:27 AM
How to generate random numbers? Ox1gEN Scripting Help 5 03/09/2014 03:50 PM
/work = generate a random checkpoint BleverCastard Scripting Help 1 14/05/2012 09:17 PM
[Include] Insanity Random Lists(generate a random number from your list) CyNiC Includes 10 23/07/2011 01:45 PM


All times are GMT. The time now is 06:39 AM.


Powered by vBulletin® Version 3.8.6
Copyright ©2000 - 2019, Jelsoft Enterprises Ltd.