AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |
Back to Blog
While it may not always feel this way, nothing a computer does is random, so creating a truly random number generator is a challenging problem to tackle. To see this in action, try running the following simple program without seeding the math/rand package. This also means that if another package were to always seed the math/rand package with another number (like 42), you would also get similarly predictable results every time you restart your applicatoin. If you ever opt to not create a rand.Rand object in your code and instead use the methods provided by the math/rand package, beware that the default seed value is 1, so if you forget to seed it you will find that your “random” package is really generating the same sequence of numbers every time you run your application. A source is basically just an object that helps us get randomly distributed numbers using a seed that we provide. We initialize this variable using the rand.New() function, which requires a rand.Source as an argument. By using a rand.Rand instance we are able to prevent this from happening to our random number generator. This is important, because another piece of code we import might also seed the math/rand package and cause all of our “random” functions to not really be that random.įor example, if we seed with rand.Seed(time.Now().UnixNano()) and then another initializer calls rand.Seed(1) our seed will get overridden, and that definitely isn’t what we want. This type has almost all of the functions available in the math/rand package, but we are able to isolate it so that no other code can affect our seed. The first few lines should look familiar to anyone who has written some Go code before - we start by declaring our package, and then follow it with a few imports stating the packages we will be using in our code.Īfter that we declare a global variable named seededRand with the type *rand.Rand. Package rand import ( "math/rand" "time" ) var seededRand * rand. In it we are going to start by writing an init() function that handles seeding the math/rand package. Open rand/strings.go if you haven’t already. As of now that is the entirety of our package, but you might find yourself updating this package over time so it is good to start with that in mind. In the rand/strings.go file we are going to store all of our functions related to random strings. For now we will only have functions related to strings, but you are welcome to add to the package over time as your project needs evolve. The rand directory is going to store all of our code that is part of the rand package we are creating. Once you do that, create a file named strings.go in the newly created directory. This is going to change based on your local environment, but I suggest creating a folder named rand inside of whatever directory you are working in. The first thing we are going to is create a directory to store our new package. Side Note: I have written about generating random string in the past as part of a list of tips for using strings in Go, but I felt that the topic deserves its own post as it is a pretty common request on its own. If you do happen to find yourself still using the math/rand package in other parts of your application, it might be worth considering either moving some of that code into a function in your new rand package so that it is easier to reuse and test in isolation. It might seem odd to wrap another package with the same package name, but in my experience (and in others’ experience) this pattern works well when you want to wrap a package based on the context of what you are building, or if you want to isolate some details that just aren’t relevant to the rest of your application. That way the rest of our code doesn’t need to concern itself with the implementation details of generating random strings, but can instead simply call functions like rand.String(10) to get a random string with 10 characters in it. What this means is we are going to create a custom package named rand that will utilize the functionality provided by the math/rand package in order to create our own functions, and mask most of the implementation details. String() - this function will only take in a length, and will use a default characters set to generate a random string.StringWithCharset() - this function will take in a character set and a length and will generate a random string using that character set.To do this, we will write a short rand package that wraps the math/rand package and provides the following two functions: In this post are we going to cover how to create a function that will allow us to generate random strings of any length in our Go code.
0 Comments
Read More
Leave a Reply. |