Random numeric strings
#1

I need a way to generate random strings consisting of numeric characters only. I searched and went through over 10 pages but couldn't find anything. Can anyone assist me?
Reply
#2

Couldn't you just use the random function a few times? If I read correctly all you want is a string of numbers right? If you don't care about the length you could just use something like random(500000); and if you do care about the length, just use random(9) a few times and then put them all in a string.
Reply
#3

Wouldn't that be inefficient?
Reply
#4

pawn Code:
format(string,sizeof(string),"%d",random(9999));
format(string,sizeof(string),"%d%d%d%d",random(9),random(9),random(9),random(9));
Reply
#5

Unless someone else responds with a better solution I can just use the code "varthshenon" provided. Thanks!
Reply
#6

Another.
pawn Code:
valstr(string,random(9999));
IDK which one is faster.
Reply
#7

Quote:
Originally Posted by varthshenon
View Post
Another.
pawn Code:
valstr(string,random(9999));
IDK which one is faster.
I need the string to end up being 6 characters total. I'm just doing this:

pawn Code:
stock randNumber()
{
    new szString[13];
    format(szString, sizeof(szString), "%d%d%d%d%d%d", random(9), random(9), random(9), random(9), random(9), random(9));
    return szString;
}
Reply
#8

Oh okay. Btw the length is 6, not 12.
Reply
#9

Quote:
Originally Posted by varthshenon
View Post
Oh okay. Btw the length is 6, not 12.
I doubled it because I wasn't sure if each "%d" would count as two characters. I'll change it to 7.
Reply
#10

Why you use 9? isnt it 10?

0,1,2,3,4,5,6,7,8,9 = 10 not 9

Sorry if im wrong...
Reply
#11

Yes it's 10. What a stupid I am.
Reply
#12

Quote:
Originally Posted by DRIFT_HUNTER
Посмотреть сообщение
Why you use 9? isnt it 10?

0,1,2,3,4,5,6,7,8,9 = 10 not 9

Sorry if im wrong...
It's 9 because it would be numbers 0-9 and not 1-9. I am getting strings like "840824" so clearly all numbers are working properly.
Reply
#13

No, it'd be 10.. random(10) would be from 0-9.
Reply
#14

Quote:
Originally Posted by PrawkC
Посмотреть сообщение
No, it'd be 10.. random(10) would be from 0-9.
Wrong. I am absolutely positive it's 9. I want the numbers 0 - 9 and not 0 - 10. It would mess up my code if one of the digits generated to 10.
Reply
#15

Sorry to say, but you're wrong.
Give the wiki a read
https://sampwiki.blast.hk/wiki/Random
"Returns A random number ranging from 0 to max-1."
Reply
#16

Quote:
Originally Posted by PrawkC
Посмотреть сообщение
Sorry to say, but you're wrong.
Give the wiki a read
https://sampwiki.blast.hk/wiki/Random
"Returns A random number ranging from 0 to max-1."
Okay, you got me there. I could of looked but I was too lazy! I'll change it to 10, sorry if I came across as being arrogant.
Reply
#17

Heres a more efficient way to get a random string, calling random only once.

pawn Код:
#define RandomEx(%1,%2) (random(%2-%1)+%1)// RandomEx(min, max)

public OnFilterScriptInit()
{
    new
        szStr[11];
    format(szStr, sizeof(szStr), "%i", RandomEx(1000, 9999));
    print(szStr);
    return 1;
}
Failed twice with the format... Its too early.
Reply
#18

or u can use
pawn Код:
format(string,sizeof(string),"%d",100000+random(899999));
Reply
#19

6 characters? You could also use

pawn Код:
szString = random(99999)+100000;
Reply
#20

Quote:
Originally Posted by Y_Less
Посмотреть сообщение
a) Use valstr for strings consisting purely of a number.

b) marrcko's code was the closest, but still not quite right for the same reason "random(9)" wasn't quite right:

pawn Код:
new
    str[7]
valstr(str, random(900000) + 100000);
That will generate a random number in the interval [0,899999], previous code did [0,899999), then add 100000 to give [100000,999999] (i.e. all the 6-digit numbers). Note that this excludes numbers like 000001, if you want those too:

pawn Код:
new
    str[7]
format(str, sizeof (str), "%06d", random(1000000));
In this case "format" is the better option as you're using more advanced features.
Thanks for the response, Alex.
Reply


Forum Jump:


Users browsing this thread: 1 Guest(s)