![]() ![]() LetterIdxBits = 4 // 4 bits to represent a letter indexĬache, remain = src1. What is the fastest and simplest way to generate a lot of a random hex string of a fixed length in Go? If idx := int(cache & letterIdxMask) idx >= letterIdxBitsįor i := 0 i = 0, but I don't find why there is this error. LetterIdxBits = 6 // 6 bits to represent a letter indexĬache, remain = src.Int63(), letterIdxMax I'm doing something like this: const letterBytes = "abcdef0123456789" Warning: Without the call to rand.Seed you will get the same sequence of pseudorandom numbers each time you run the program. I find this solution How to generate a random string of a fixed length in golang? With an identifier of only 22 characters in length.I need to generate a lot of a random hex string of a fixed length. Just truncate these, without any impact on the uniqueness. The Base64 representation ends with the '=' characters. 'iYgk5hIqWkCwbZaVuCwKnA=' (string length: 24) Length: 36) will get a Base64 representation: Var messageID = Convert.ToBase64String(newGuid.ToByteArray()) Representation using Base64 resulted in a useable and still unique We found that converting the Guid (16 bytes) to an ASCII OK for database, not great for URLs, but it helped me appreciate the other answers :-) Sadly, it sometimes includes + and / characters. I really like the simplicity of just encoding a GUID using Base64 format and truncating the trailing = to get a string of 22 characters (it takes one line of code, and you can always convert it back to GUID). Hoping this may help someone else looking for short unique IDs. Suid-server-java - Suid-server implementation for the Java EE technology stack.īoth libraries are available under a liberal Creative Commons open source license. ![]() Implementing servers in other languages should be easy as well.ĭistributed Service-Unique IDs that are short and sweet I have published both a Javascript library for the client side, as well as a Java EE server implementation. IDs can be generated locally for a very long time before needing contact with the server again (depending on pool settings).Base 36 encoded so very easy for humans to read, write and pronounce.Stays within 53 bits (Javascript / PHP max size for integer numbers).IDs are more or less predictable (less so than regular DB ids but they aren't random).When the pool runs low it fetches more ID blocks from the server to fill it up again. ![]() The local ID generator maintains a pool of ID blocks to generate an ID every time one is requested.A central server issues ID blocks consisting of 32 IDs each. ![]() Finally, we printed a random string of length 10 in the main function. The randomString () is a convenience function that calls stringWithCharset with the default character set. So I ended up rolling my own based on the technique Flickr uses, but modified to require less coordination and allow for longer periods offline. Using the provided character set, the stringWithCharset () function generates a random string of a given length. I could not really find anything that was not based on random and still very short. Random number generator (RNG) generates a set of values that do not display any distinguishable patterns in their appearance. go version go version go1.18.1 linux/amd64 We use Go version 1.18. I looked into available libraries but most of them are based on randomness and I didn't want that. Go random tutorial shows how to generate random values in Golang. Public static string GetBase36(int length) Public static string GetBase62(int length) Private static Random _random = new Random() "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz" create five IDs of eight base 36 charactersįor (int i=0 i<5 i++) Console.WriteLine(RandomIdGenerator.GetBase36(8)) create five IDs of six, base 62 charactersįor (int i=0 i<5 i++) Console.WriteLine(RandomIdGenerator.GetBase62(6)) (See code at bottom of answer)ĥ chars in base 62 will give you 62^5 unique IDs = 916,132,832 (~1 billion)Īt 10k IDs per day you will be ok for 91k+ daysĦ chars in base 62 will give you 62^6 unique IDs = 56,800,235,584 (56+ billion)Īt 10k IDs per day you will be ok for 5+ million daysĦ chars will give you 36^6 unique IDs = 2,176,782,336 (2+ billion)ħ chars will give you 36^7 unique IDs = 78,364,164,096 (78+ billion)Ĭode: public void TestRandomIdGenerator() Of course you will have to store the created IDs and check for duplicates on creation to ensure uniqueness. It's a well-understood method for creating "unique", human-readable IDs. Base 62 is used by tinyurl and bit.ly for the abbreviated URLs. ![]()
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |