![]() ![]() Maybe you can elaborate on your use case to clarify your requirements. It will also be challenging in terms of complexity if your use case does not determine the quantity of secrets required during the lifetime of the system.įor me, it really boils down to the question whether the identifiers for your system you use for info lookups are really secrets or you just want unique identifiers which are hard to predict before they occur in the system. ![]() However, this approach may introduce a new challenge depending on your setting as the numbers are now kept in memory which is insecure per se. While recording the numbers, duplicates can be detected and a new random can be requested in case. Package randutil provides methods to generate random strings and salts. If you really need true uniqueness you may want to combine random numbers with a map to record them, where the number serves as key and the value is a "don't care". If you are curious about the probability of duplicate UUIDs, see Wikipedia for example. The probability of this to happen is very low, however, and it may be good enough for your use case. If nobody can answer that, I'll plot a 2-D scatter plot between the two and find out myself.With crypto/rand there is no guarantee that individual random numbers will occur more than once. It is equivalent of what I'm doing now, and it doesn't answer my real question, "Regarding randomness of the full 8-digits, would the two be the same, or there is really one better than the other? " To generate a random string in Go, you need to write a custom function that randomly selects characters from a given charset as many times as the set length of the output and combines them to form a random, fixed-length string. CREATE AS SELECT GO CREATE MinLength int. Please do not provide low + rand(hi-low) as the answer, as everyone knows that. While controling their length and the type of chars they can contain. Is it ture, or it is only a wishful thinking? Regarding randomness of the full 8-digits, would the two be the same, or there is really one better than the other? Random numbers are generated by a Source. Using rand function Package rand implements pseudo-random number generators unsuitable for security-sensitive work. I.e., I guess Int63n might be better for my case than Intn. In Golang, with the help of the rand package, we can easily write a custom function to generate a random string with the desired length. There are standards such as UUID (excluding v4) that use other methods to try to 'guarantee' uniqueness of each identifier. Both types should be used by a single goroutine at a time: sharing among multiple goroutines requires some kind of synchronization. Random is a small library for generating random strings. Random numbers are generated by a Source, usually wrapped in a Rand. So I'm thinking, for this specific problem, would the following be the fastest and simplest way to do it? 99999999 - rand.Int63n(90000000) 3 Answers Sorted by: 5 Of course with randomly generated tokens, there is always the possibility of generating a duplicate token. Overview Package rand implements pseudo-random number generators unsuitable for security-sensitive work. I.e., I care about the the quality of the randomness more in the sense of its length. Say to generate 8-digits long numbers, the problem with rand.Intn(100000000) is that the result might be far less than 8-digits, and padding it with leading zeros doesn't look like a good answer to me. What is the fastest and simplest way to generate fixed length random numbers in Go? It takes input a number n and will return a number x in range 0 ![]()
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |