SA-MP Forums Generate random
 User Name Remember Me? Password
 New Account Members List Search Today's Posts Mark Forums Read

 Thread Tools Display Modes
 28/03/2019, 08:44 PM #1 SymonClash Banned   Join Date: Dec 2018 Posts: 257 Reputation: 25 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.
 28/03/2019, 11:11 PM #2 TokicMajstor Big Clucker     Join Date: Oct 2018 Location: Bosnia And Herzegovina Posts: 177 Reputation: 1 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 __________________ SUBSCRIBE TO PEWDIEPIE 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
 29/03/2019, 03:07 AM #3 Crayder Banned   Join Date: Sep 2013 Location: Flames of Hell Posts: 3,370 Reputation: 652 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.
29/03/2019, 08:22 AM   #4
TheToretto
Gangsta

Join Date: Jul 2015
Posts: 596
Reputation: 66
Re: Generate random

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 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

 29/03/2019, 08:24 AM #5 SymonClash Banned   Join Date: Dec 2018 Posts: 257 Reputation: 25 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
 29/03/2019, 04:45 PM #6 bgedition Huge Clucker     Join Date: Oct 2013 Location: 127.0.0.1 Posts: 422 Reputation: 37 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.[Tutorial] ZCMD: How to make 'forcecmd' command.
30/03/2019, 06:50 AM   #7
Crayder
Banned

Join Date: Sep 2013
Location: Flames of Hell
Posts: 3,370
Reputation: 652
Re: Generate random

lol

This is essentially correct...
Quote:
 Originally Posted by bgedition 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`

 30/03/2019, 12:00 PM #8 Nero_3D High-roller     Join Date: Jun 2007 Location: Germany Posts: 2,813 Reputation: 650 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 thatstr[i] = rand + ((rand < 10) ? (48) : (55));`
30/03/2019, 04:31 PM   #9
Crayder
Banned

Join Date: Sep 2013
Location: Flames of Hell
Posts: 3,370
Reputation: 652
Re: Generate random

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 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 thatstr[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

 Thread Tools Display Modes Linear Mode

 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 Forum Rules

 Similar Threads Thread Thread Starter Forum Replies Last Post ServerFiles Scripting Help 1 30/05/2018 12:23 PM Bussyman Scripting Help 1 15/04/2017 10:27 AM Ox1gEN Scripting Help 5 03/09/2014 03:50 PM BleverCastard Scripting Help 1 14/05/2012 09:17 PM CyNiC Includes 10 23/07/2011 01:45 PM

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

 -- (EN) English -- (RU) Русски Contact Us - SA-MP - Archive - Top