Generating random characters? - Printable Version
+- SA-MP Forums Archive (
https://sampforum.blast.hk)
+-- Forum: SA-MP Scripting and Plugins (
https://sampforum.blast.hk/forumdisplay.php?fid=8)
+--- Forum: Scripting Help (
https://sampforum.blast.hk/forumdisplay.php?fid=12)
+--- Thread: Generating random characters? (
/showthread.php?tid=585370)
Generating random characters? -
sjames - 13.08.2015
Hi.. I want to generate some random letters and then put them into textdraw.
For example, it want it to generate:
X 4 K 8 S K O W M S N P Q
And then I put it into a textdraw string.. how do this?
Re: Generating random characters? -
Isolated - 13.08.2015
pawn Код:
GenerateRandomLetters(len) {
new Letters = ['A','B','C','D','E','F','G','H','I','J','L','M','N','O','P','Q','R','S','T','U','V','W','X','Y','Z'];
new Ret[len];
for(new i = 0; i < len; i++) {
format(Ret, sizeof(Ret), "%s %s", Ret, Letters[i]);
}
return Ret;
}
Something like this - untested - I'm a PHP developer, may be a little wrong.
Re: Generating random characters? -
LetsOWN[PL] - 13.08.2015
Quote:
Originally Posted by Isolated
pawn Код:
GenerateRandomLetters(len) { new Letters = ['A','B','C','D','E','F','G','H','I','J','L','M','N','O','P','Q','R','S','T','U','V','W','X','Y','Z']; new Ret[len]; for(new i = 0; i < len; i++) { format(Ret, sizeof(Ret), "%s %s", Ret, Letters[i]); } return Ret; }
Something like this - untested - I'm a PHP developer, may be a little wrong.
|
You can not create arrays with non-fixed (non-constant) size - it must be known for compiler while it's compiling the code.
Also, using arrays is quite slow, this one is better (I just wrote it and tested - working):
pawn Код:
// Generate random character (a-z A-Z only)
stock randomchar()
return ( random(1000) %2 ==0 ) ? (65 + random(26)) : (97 + random(26));
Example usage:
pawn Код:
main() {
new RandomString[16];
for(new i = 0; i < sizeof(RandomString); i++)
format( RandomString, sizeof( RandomString ), "%s%c", RandomString, randomchar());
printf("%s", RandomString);
}
//Possible output: AswHqLkMEJxCNte
Edit.
A little modification to the generator, so that it can also generate digits (if you want so)
pawn Код:
stock randomchar( bool:useDigits = false ) {
if( useDigits ) {
if( random(1000)%2==0 )
return 48 + random(10);
else
return (random(1000)%2==0) ? (65 + random(26)) : (97 + random(26));
} else
return ( random(1000) %2 ==0 ) ? (65 + random(26)) : (97 + random(26));
}
Greetings.
Re: Generating random characters? -
sjames - 13.08.2015
Thanks a lot. Sent you another PM aswell.
Re: Generating random characters? -
Vince - 13.08.2015
Quote:
Originally Posted by LetsOWN[PL]
A little modification to the generator, so that it can also generate digits (if you want so)
pawn Код:
stock randomchar( bool:useDigits = false ) { if( useDigits ) { if( random(1000)%2==0 ) return 48 + random(10); else return (random(1000)%2==0) ? (65 + random(26)) : (97 + random(26)); } else return ( random(1000) %2 ==0 ) ? (65 + random(26)) : (97 + random(26)); }
Greetings.
|
A little nitpicking here, but mod is inherently very slow. If you want to check if a number is odd, you may use:
pawn Код:
isOdd(number) return number & 1;
Also using 'A' and 'a' instead of 65 and 97 may make the intentions a bit more clear.