ID:155407
 
I wanted to assign a random ID to each new player once they create their character, and join the world.

Admins will be referring alot to this ID, for strictly reward purposes, as well as my npc's will target certain IDs that they interact with(already working on that)


What i need to know, is how to generate this ID?

I needs to be 100% random, and probably consist of 5 digits.

Do you want your IDs to be all numbers, all letters or a mix of both?
thanks!
In response to Komuroto
Better way of doing this would to just use the "mob" variable, or if you really want random numbers, the "tag" variable.
In response to Emasym
NOT really trying to get it too complicated.

But explain your comment?
Your key is a unique id btw.
In response to ExPixel
I know that, but theres a reason i wanted the ID code.
In response to Komuroto
We'll then do something using the player's key so it looks cool or whatever but is still unique like...text2ascii or binary or ever Hex idk.
In response to ExPixel
I got the idea locked in my head ;)
In response to Komuroto
The thing is, even when using rand(1,9999999), there is no guaranteeing it will be unique.

And a datum's tag variable is used for exactly this, no reason to define another variable.

for(var/i=1,i<length(ckey),i++)
tag+=num2text(text2ascii(ckey,i))


This is guaranteed to be unique (since ckey is unique), and the advantage is 'tag' is that you can use locate(tag) to get the mob.

However, this is definitely not limited to 5 chars, but hey, I'm just pointing out.
In response to Emasym
Does he need number or string? Code you provided won't generate unique numbers
In response to Zaoshi
This is because 'tag' acts as a string. Locating numbers would be silly.

Also, if it were a number, it would add the numbers mathematically, which would still not be a completely unique number.

If it really does need to be a number in the end (the variable should be changed then, NOT 'tag'), just place

id = text2num(id)


After the for()
In response to Emasym
Emasym wrote:
The thing is, even when using rand(1,9999999), there is no guaranteeing it will be unique.

And a datum's tag variable is used for exactly this, no reason to define another variable.

> for(var/i=1,i<length(ckey),i++)
> tag+=num2text(text2ascii(ckey,i))
>

This is guaranteed to be unique (since ckey is unique), and the advantage is 'tag' is that you can use locate(tag) to get the mob.

However, this is definitely not limited to 5 chars, but hey, I'm just pointing out.

I didn't even consider this to be honest. Nice efficiency. ;)
What I would've done is this:

var/last_id = 0

mob
var/id

Login()
if(isnull(id))
id = last_id +1
last_id ++
..()


This way, there will never ever be duplicate IDs.
(last_id is a "global" variable, you will need to save and load it at world/Del() and world/New().)
In response to Komuroto
What i didn't get is why didn't you do this since u wanted to have it random and at fixed 5 digits its abit long winded but hey though i would show it. Firstly make a list varible that saves somewhere like knownID and have that saved/loaded at world new and delete. then have something like this

DontEnd = 1 //just a control varible for the while loop
while(DontEnd)
var/NewID = rand(1,99999)
if(!(NewID in knownID))
src.ID = NewID
knownID += NewID
DontEnd = null

it is abit pointless but though it might be an option