• 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
[Pawn] Random letters and numbers
#1
Hi guys. could you help me generate numbers and letters in the same string?



Does not work. how could i do?



Code:
stock CreateVehiclePlate()

{

? ? new string[10];

? ? format(string, sizeof(string), "%s %d %s %s",? random('A'-'Z'), random('0'-'9'), random('A'-'Z'), random('A'-'Z'));

? ? return string;

}
  Reply
#2
PHP Code:
printf("Random capital letter: %s",? 'A'  random('Z'-'A'  1));

printf("Random digit: %d"random(10)); 
Always keep in mind that a lot of people are active on this forum in their spare time.

They are sacrificing time they could easily spend on things they would rather do, to help you instead.

  Reply
#3
(2020-11-25, 11:20 PM)Freaksken Wrote:
PHP Code:
printf("Random capital letter: %s",? 'A'  random('Z'-'A'  1));

printf("Random digit: %d"random(10)); 



It doesn't work when I use everything in the same string. need to generate vehicle plate.



Code:
format(string, sizeof(string), "%s %d %s %s",? 'A'  random('Z'-'A'  1), random(10), 'A'  random('Z'-'A'  1), 'A'  random('Z'-'A'  1));
  Reply
#4
When printing a single character (which is just a number), use %c (which stands for character, obviously). %s is for strings (arrays).
Also be more specific. "It doesn't work". What doesn't work? Does it print weird characters? (Which is likely since you used specificer s instead of c). Or does it not work at all? If so, there is another problem going on.

Quote:random('0'-'9')
https://open.mp/docs/scripting/functions/random
random() takes as parameter "max", not a "range". For example, if the input is 15 it will return a random value from 0 up to 14. Negative values (0-9 = -9) will print large values.
When wrapping something in single quotes (eg. 'a', '9') it will return the numeric value of that character. For example, '5' is 5, but 'a' has numeric value 97. So when using:
Code:
'A'  random('Z' - 'A'  1)
You're actually doing:
Code:
65  random(90 - 65  1) //65  random(26): Output will be 65  (0-25), so output will be atleast 65 ('A') and maximal 65(90) ('Z')
Note that these character numbers are ASCII numbers (if I'm not wrong): http://www.asciitable.com/ (I believe SAMP covers 128 of them?)
Quote:Computers can only understand numbers, so an ASCII code is the numerical representation of a character such as 'a' or '@' or an action of some sort
  Reply
#5
(2020-11-26, 02:27 AM)Kwarde Wrote: When printing a single character (which is just a number), use %c (which stands for character, obviously). %s is for strings (arrays).

Also be more specific. "It doesn't work". What doesn't work? Does it print weird characters? (Which is likely since you used specificer s instead of c). Or does it not work at all? If so, there is another problem going on.



Quote:random('0'-'9')

https://open.mp/docs/scripting/functions/random

random() takes as parameter "max", not a "range". For example, if the input is 15 it will return a random value from 0 up to 14. Negative values (0-9 = -9) will print large values.

When wrapping something in single quotes (eg. 'a', '9') it will return the numeric value of that character. For example, '5' is 5, but 'a' has numeric value 97. So when using:

Code:
'A'  random('Z' - 'A'  1)

You're actually doing:

Code:
65  random(90 - 65  1) //65  random(26): Output will be 65  (0-25), so output will be atleast 65 ('A') and maximal 65(90) ('Z')

Note that these character numbers are ASCII numbers (if I'm not wrong): http://www.asciitable.com/ (I believe SAMP covers 128 of them?)

Quote:Computers can only understand numbers, so an ASCII code is the numerical representation of a character such as 'a' or '@' or an action of some sort





I apologize. I do not speak English and there may have been an error in the translation.

The problem that is happening is that it is printing strange characters in the letters and numbers.



Thank you all very much for your attention. Reach this way with support on the discord.



Credit: Dobby#7939 (Discord)



Code:
#define RandomEx(%0,%1) (random(%1 - %0  1)  %0)



format(numberplate, sizeof(numberplate), "%c%c%c%c %c%c%c", RandomEx('A','Z'), RandomEx('A','Z'), RandomEx('1','9'), RandomEx('1','9'), RandomEx('1','9'), RandomEx('A','Z'), RandomEx('A','Z'));
  Reply


Forum Jump: