Getting length of an integer?
#1

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?)
Reply
#2

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
Reply
#3

Yes, I have adapted to that method - I still want to know if there is a function to do what I have asked.
Reply
#4

by default - no. You would need to create a custom function for that.
Reply
#5

PHP код:
new randnum;
Rand
    
randnum random(9999999);
    if(
randnum 10000000) { goto Rand; }
PlayerData[playerid][PhoneNumber] = randnum
Or:

PHP код:
new randnumstr[8];
Rand
    
randnum random(9999999);
    
format(strsizeof str"%d"randnum);
    if(
strlen(str) != 7) { goto Rand; }
PlayerData[playerid][PhoneNumber] = randnum
Reply
#6

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.
Reply
#7

You can
pawn Код:
while(randnum < 1000000)
Reply
#8

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.
Reply
#9

Quote:
Originally Posted by Jefff
Посмотреть сообщение
You can
pawn Код:
while(randnum < 1000000)
Should not be so?

PHP код:
while(randnum 1000000
Reply
#10

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).
Reply


Forum Jump:


Users browsing this thread: 1 Guest(s)