Getting length of an integer? - 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)
+---- Forum: Help Archive (
https://sampforum.blast.hk/forumdisplay.php?fid=89)
+---- Thread: Getting length of an integer? (
/showthread.php?tid=268252)
Getting length of an integer? -
jameskmonger - 11.07.2011
How can I get the length of an integer? I am trying to make a system to generate a random 7 digit phone number, and I am planning to use something like this:
pawn Код:
new randnum;
while (intlen(randnum) != 7) {
randnum = random(9999999);
}
PlayerData[playerid][PhoneNumber] = randnum;
But I can't find a function to calculate the length of the value though.
(My while loop could be wrong, too, it is meant to keep generating random numbers until it hits a 9 digit one, and then set the player's phone number to that - am I doing it right?)
Re: Getting length of an integer? -
Roko_foko - 11.07.2011
If I understood good, you want to make random phone numbers starting with 1000000 to 9999999? If yes
num=1000000+random(9000000); EXPLANATION: min_num =1000000+0, max_num = 1000000+9999999=9999999
Re: Getting length of an integer? -
jameskmonger - 11.07.2011
Yes, I have adapted to that method - I still want to know if there is a function to do what I have asked.
Re: Getting length of an integer? -
Macluawn - 11.07.2011
by default - no. You would need to create a custom function for that.
Re: Getting length of an integer? -
Shadoww5 - 11.07.2011
PHP код:
new randnum;
Rand:
randnum = random(9999999);
if(randnum < 10000000) { goto Rand; }
PlayerData[playerid][PhoneNumber] = randnum;
Or:
PHP код:
new randnum, str[8];
Rand:
randnum = random(9999999);
format(str, sizeof str, "%d", randnum);
if(strlen(str) != 7) { goto Rand; }
PlayerData[playerid][PhoneNumber] = randnum;
Re: Getting length of an integer? -
jameskmonger - 11.07.2011
pawn Код:
new randnum, str[8];
Rand:
randnum = random(9999999);
format(str, sizeof str, "%d", randnum);
if(strlen(str) != 7) { goto Rand; }
PlayerData[playerid][PhoneNumber] = strval(randnum);
Thanks, strval added to remove tag mismatch warnings.
Re: Getting length of an integer? -
Jefff - 11.07.2011
You can
Re: Getting length of an integer? -
RyDeR` - 11.07.2011
You don't need all of that to generate a random 7 digit "phone" number.
Check this out:
https://sampforum.blast.hk/showthread.php?tid=266154#4
EDIT: Then just use strval to make an integer.
Re: Getting length of an integer? -
Shadoww5 - 12.07.2011
Quote:
Originally Posted by Jefff
|
Should not be so?
PHP код:
while(randnum > 1000000)
Re: Getting length of an integer? -
Calgon - 12.07.2011
You can use more than and less than operators.
You should completely refrain from using 'goto' statements anywhere in your code (there's actually quite a valid reason, other than the dinosaur).