SA-MP Forums Archive
Random numeric strings - 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: Random numeric strings (/showthread.php?tid=277462)



Random numeric strings - Scenario - 18.08.2011

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?


Re: Random numeric strings - JaTochNietDan - 18.08.2011

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.


Re: Random numeric strings - Scenario - 18.08.2011

Wouldn't that be inefficient?


Re: Random numeric strings - =WoR=Varth - 18.08.2011

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));



Re: Random numeric strings - Scenario - 18.08.2011

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


Re: Random numeric strings - =WoR=Varth - 18.08.2011

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


Re: Random numeric strings - Scenario - 18.08.2011

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;
}



Re: Random numeric strings - =WoR=Varth - 18.08.2011

Oh okay. Btw the length is 6, not 12.


Re: Random numeric strings - Scenario - 18.08.2011

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.


Re: Random numeric strings - DRIFT_HUNTER - 18.08.2011

Why you use 9? isnt it 10?

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

Sorry if im wrong...


Re: Random numeric strings - =WoR=Varth - 18.08.2011

Yes it's 10. What a stupid I am.


Re: Random numeric strings - Scenario - 18.08.2011

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.


Re: Random numeric strings - PrawkC - 18.08.2011

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


Re: Random numeric strings - Scenario - 18.08.2011

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.


Re: Random numeric strings - PrawkC - 18.08.2011

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."


Re: Random numeric strings - Scenario - 18.08.2011

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.


Re: Random numeric strings - iggy1 - 18.08.2011

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.


Re: Random numeric strings - marrcko - 18.08.2011

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



Re: Random numeric strings - TouR - 18.08.2011

6 characters? You could also use

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



Re: Random numeric strings - Scenario - 18.08.2011

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.