4 digit unique number generator

Hi everyone,

I am working on an embedded linux project that requires me to generate a unique 4 digit code based on my device id. My device ID could go from 1 - 1024. Is there a simple mathametical formula that I could use to generate the required number. I try to write something on my own and I am not successful at generating unique numbers.

Any help is much appericiated! Thanks everyone!

EZ

Reply to
linuxdude
Loading thread data ...

If device IDs are not unique, how to expect to generate unique numbers based on them? If device IDs are unique, then just use the device ID.

--
Grant Edwards
grante@visi.com
Reply to
Grant Edwards

Sorry, I didn't explain myself well here. The device ID's are unique. Treat these generated numbers as passwords & we don't want to make it obvious .

EZ

expect to generate unique

Reply to
linuxdude

lookup "hash functions"

Reply to
noone
1.. 1024 is 10 bits

9999 is 10011100001111 -> 14 bits

so insert four constant bits in 4 arbitrary bit positions. e.g.

x = id-1; x0 = x & 0x0003; // bits 1, 0 x1 = (x & 0x001C)

Reply to
Michael Schnell

evensimpler: strtching:

(id*9999)/1024

-Michael

Reply to
Michael Schnell

...or just (id+3).

:)

Reply to
Anders

We've lost the OP's desire that it not be a simple relationship.

I like the bit-insertion technique.

You can even shuffle the bits around a bit.

And for the inserted bits, don't used constants, but another bit already in the id. You are going to drop those bits to "decode" it anyway.

4 digits? I suppose 0-8191 is acceptible (2^13) or better, (1000-9191) so they are no leading zeros in the passkey.

and 1-1024 (0-1023) is (2^10)

// // just make sure you include every bit 0-9 at least // once in the array //

char bitshuffle[13] = {0,2,4,6,8,1,3,5,7,9,1,2,3);

int gencode(int id) { int code = 0; for (int i=0; i

Reply to
Rufus V. Smith

ElectronDepot website is not affiliated with any of the manufacturers or service providers discussed here. All logos and trade names are the property of their respective owners.