Generating a GUID
BlitzMax Forums/BlitzMax Programming/Generating a GUID
| ||
Does anyone know how to generate a GUID in code rather than use a library? |
| ||
A Global Unique IDentifier? I've been using the memory address of where the ID variable is stored, as the variables value, for an ID number, but that only really applies locally. You could then append the ID of a computer to it, such as its IP address as an int, giving you a Long containing the IP address and the memory address. Otherwise you could have one computer be a `GUID server` which generates a number and then sends the number to the other computers for them to use. |
| ||
I like your approach in using the memory address, but I don't want to get into using the IP address because of Privicy problems that other GUID systems have had. I have tried the following code, but it appears to run too fast, as the numbers are not random enough... SuperStrict For Local t%=1 To 10 Print createguid() Next For Local t%=1 To 10 Print createguid( True ) Next '# Create a random GUID (with optional dashes) '# NOT compatible with the Windows one. Function createGUID$( lDashes% = False) Local guid$ = "" Local nibble% Const HEXDIGIT$ = "0123456789ABCDEF" SeedRnd MilliSecs() For Local digit% = 1 To 32 nibble = Int( Rnd(16) ) guid:+ HEXDIGIT[nibble..nibble+1] Next If lDashes Then guid = guid[..8]+"-"+guid[8..12]+"-"+guid[12..16]+"-"+guid[16..20]+"-"+guid[20..] End If Return "{"+guid+"}" End Function Alternatively I can move the SeedRnd command to the top, but that kind of defeats the object of having all the code in one place... |
| ||
Several simple hacky solutions occur to me. The first and most direct solution to something running "too fast" is to throw in a delay(), maybe delay(Rand(1, 2)). Another solution is to produce a random number of random numbers each loop and randomly (wee!) select one of those for use. Seriously, though, as long as you aren't producing these numbers a billion at a time, a simple delay() will probably do the trick. SpaceAce |
| ||
more of a GID then.... to make it unique I'd add in somthing like the MAC address of their Ethernet card which *should* be unique, IP addresses privacy aside can be dynamic and therefore not unique. serial numbers from other hardware could be used but cpuID's for example are often masked by the BIOS |
| ||
You probably want to read this: http://www.opengroup.org/dce/info/draft-leach-uuids-guids-01.txt |
| ||
Thanks Winni, that is a really useful link. Can I obtain the local timezone from Blitz? CurrentTime() seems to only use "local" time. Is it possible to obtain the MAC address of the local Network adapter in Blitz? Cheers |
| ||
on most linix and maybe just mac platforms just cat /proc/net/arp will show you the active hardware address in windows you'll probably have to do all sorts of api hoop jumping to find it... a simpler way might be to run ipconfig /all using the freeprocess mod and and parse the output (I'd be tempted to do the same for linux too) could a mac user tell me if you can cat /proc/net/arp or some similar they do have a command line on mac's dont they :o) |
| ||
I have been giving some thought to the need for a global unique ID since I recently realized that I need this myself as part of my networking system. I need to be able to create objects on each computer with completely unique ID's which will not be duplicated anywhere else, regardless of whether the computers are connected or not. It seems a number of schemes are used but I'm not entirely happy with trying to use some from the hardware. I can see how the `address space` of the CPU and its boundaries are what create the need for something that goes beyond that space, and it is because the CPU and memory are separate in each computer that the problems of creating a non-duplicated ID comes about. I also philosophized somewhat and realized that as you approach uniqueness, you are approaching anonymity, and you need that anonymity in order for the uniqueness to be there, but unfortunately the more anonymous you get the less the ID has any meaning and can't be based on anything that came before. So it can't really be based on the MAC address or derived as a hash value from some input data etc. Absolute uniqueness is impossible, you can only have a reasonably high degree of uniqueness, in the hope that it is enough to represent a lot of separate objects for a long period of time without repetition. So you have to base the ID on some kind of pre-existing data, which leaves a paper trail, which means it is not anonymous and can be cracked and possibly duplicated someday. So I decided not so much to even bother trying to go for a completely anonymous ID, or one that is completely unique. Instead I think I will just go with using a centralized server that generates and knows about all ID's and transfers them to the client machines upon request. Then you can create unique ID's simply by keeping a COUNT of how many ID's have been created, where the counter is the ID. With, say, a 64-bit counter, or 128-bit using a couple of Long's and some slightly more `manual` math operations, that gives you a lot of objects to be created that you know are absolutely unique, not repeated, and *almost* meaningless/anonymous. Since the server knows all the numbers generated so far (because they are 0..Current) it will only ever produce unique ones up until the limits of the number storage. We know that even an Int could store over 2 billion unique numbers. The numbers don't really mean anything. They don't identify a specific time or place and only allude to a possible vague position in time when they were created. The more vague and meaningless the ID is the better. Random would, in a way, be even better, but there is no such thing as absolute randomness - there is always some chance of repetition. So anyway, I think I will just give up trying to make it truly unique (which is impossible) or completely anonymous (which is impossible) and just settle for a server-based generation of ID's. Simple, easy and fast. I don't think people are going to use my software in very big groups, just small groups, so it should be sufficient. I think if you were trying to have huge numbers of computers networked like the internet you'd want a more sophisticated ID system and would probably have to work with the MAC address or some cryptographic random number generator thing. |
| ||
usernames are unique? |
| ||
Kind of, only to a certain extent. There's nothing to stop you using the same name as me. |