2

I have a device and for each device I wish to generate a string of the following format: XXXXXXXX. Each X is either B, G, or R. An example is GRBRRBRB. This gives me roughly 7000 keys to work with which is enough as I doubt I'll have more devices.

I was thinking I could generate them all before hand, and dump them in a file or something, and just get the next key available from that, but I wonder if there is a better way to do this.

I know there are better ways to do it if I don't need guaranteed uniqueness but I definitely need that so I'm not sure what the best way to do it is.

tshepang
  • 12,111
  • 21
  • 91
  • 136
  • 1
    So you are going to be able to track which ones you have already allocated? And do they need to seem random? or can they be sequential? – weston Jan 20 '12 at 19:24
  • Would this do it for you? http://stackoverflow.com/questions/5053411/random-string-generation – Michael Dillon Jan 20 '12 at 19:24
  • @MichaelDillon - that won't work as it doesn't guarantee uniqueness which I need. If the number of possible generations were large I could ignore this, but 1/7000 chance of being the same key is too high. Especially when 6000 devices are rolled out, then theres actually a very good chance they won't be unique. – user1161306 Jan 20 '12 at 21:20
  • @weston - I guess they can be sequential, though it'd be nicer if it were random – user1161306 Jan 20 '12 at 21:22
  • Just generate all the string then shuffle them. Then just start grabbing them. Seems simple enough. – aquinas Mar 04 '14 at 19:34

3 Answers3

2

Treat it as a ternary representation of a number, where R=0, B=1, G=2. So when you're writing the nth ID, the first digit is R if n % 3 == 0, B if n % 3 == 1, G otherwise. The second digit is the same, except you're looking at (n / 3) % 3; then for the third digit look at (n / 3^2) % 3; etc.

Paul Eastlund
  • 6,713
  • 4
  • 18
  • 20
  • I know that part, I was speaking of the actual generation of the unique 8 digit id that guarantees uniqueness. – user1161306 Jan 20 '12 at 21:22
  • 1
    I'm not sure I understand. If you have a 1:1 mapping between #s and IDs as I've described, it seems like your ID scheme could be as simple as, "have a global counter n for how many IDs I've created, and when I get asked for a new ID, give the ternary representation of ++n." Why won't that work for you? – Paul Eastlund Jan 20 '12 at 22:14
  • Use Paul's answer to generate a list of all ids, and then randomize the order of the list. Then to get a new id, take the next one off the list. It'll be random, but unique. – weston Jan 21 '12 at 11:39
0

If your devices have any unique sequential ID available, I would implement a deterministic algorithm for that 'unique string' retrieval.

I mean something like

GetDeviceRgbString(deviceid) { // deterministic algorithm returning appropriate value using device id to choose it }

Otherwise consider storing it somewhere (depends on your environment, you gave little data about it, but that may be file, database, ... ) and marking them as used, preferably keep the data about assigned device, you may need it once.

doblak
  • 3,036
  • 1
  • 27
  • 22
0

I'm going to assume you want them unique to identify them for some type of network (internet) activity. That being the case, I would have a web service that can take care of making sure each device is unique by handing out IDs. The software on each device would see if it has an ID (stored locally), and if not, request one from the web service.

Erik Philips
  • 53,428
  • 11
  • 128
  • 150