Posts: 261
Threads: 32
Joined: Dec 2018
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.
Posts: 3,324
Threads: 96
Joined: Sep 2013
29.03.2019, 03:07
(
Последний раз редактировалось Crayder; 29.03.2019 в 03:42.
)
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).
Posts: 261
Threads: 32
Joined: Dec 2018
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
Posts: 3,324
Threads: 96
Joined: Sep 2013
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
Posts: 3,324
Threads: 96
Joined: Sep 2013
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
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