I have written a generator of strings, but I don’t know how to create a random hex string with length, for instance 100 digits, for inserting into a database. All these strings have to be same length.
How can I generate random hex strings?
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
While this answers OP’s question, if what you are looking for is random, then @danorton answer may be a better fit.
Like this:
65 is
Awhile 90 isZ. if you do not like "magic numbers" this form may be more readable:I’d make
ord()result a variable and move it out of the loop though for performance reasons:Or you could glue output of
sha1()s (three of them) and cut down to 100 chars. Or usemd5()instead (but I’d stick tosha1()).EDIT
sha1()outputs 40 chars long string,md5()32 chars long. So if you do not want to glue char by char (as in loop I gave above) try this functionwhere function argument is length of string to be returned. Call it
getId(100);