开发者

Simple random string generation function in C, please. Show me your art!

I'm working on a little (not so little actually) project in C (ref. this question) , and I need a little function in C that'd generate a unique and random string.

I need a small one that I can include as a utility function in a .c file. Please help me with your brains, and show me how smart this could be done!开发者_JAVA百科

Thanks and a virtual BIG hug for the best solution :)

EDIT: (What I just said to msw below) I actually think that I just need a random string as a key (which would look like a uuid) and I thought that'd be done with timestamps or such. (I don't want to add a bunch of files to my project just for a random key generation)

Sorry for the confusion, everyone!

EDIT2: Thank you all for your input, I'll finally use the OSSP uuid lib with SHA-1 it looks easier to implement/integrate


Don't re-invent the wheel, there is probably a function that is already written on your platform of choice: http://en.wikipedia.org/wiki/Uuid#Implementations


Another link for you - source of the libuuid from the ext2fs.


You can look at this internet draft which explains entire concept and try to implement one of the algorithm.


What about a counter?

If you are not multithreaded, just hold the actual value in a static variable. (Since you need a string you would have to do some sprintf or so to get what you need.)

If you are in fact multithreaded, when you produce your random key with sprintf glue the counter together with the thread and/or process id.


What about tmpnam ?

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜