This is not ground-breaking, folks and in fact it’s not even anything sexy, but it’s just something that’s been bugging me for a while when I play with some Java tools and code on my side: I need (very) often a way to quickly generate a text file with random integers as well as a quick way to read all of those integers into an array. There are all sorts of projects out there but either I’m looking in the wrong place or most of these projects are either too complicated for what they’re worth or they only do a small bit of what I need. So I set off to write this un-sexy, nothing-special piece of code to allow me to quickly generate these files so I can carry on with my work and experimenting in Java.
The idea behind it as I said is very simple: I want to be able to specify a file, a number of random integers to be generated and optionally a min/max interval. And I need a client to quickly read all of those numbers in memory in an array. (Yes, I know for some large files I shouldn’t look at doing this, but remember this utility is only to allow me to play with “stuff” and as such I won’t be operating on large data sets, so assuming that I can load all the numbers in memory is actually good enough.)
I’ve put the code on GitHub (https://github.com/liviutudor/RandomIntFile) and the moment I’ve published it I realised that I’ll probably expand it soon with a way to generate random
double‘s and maybe even strings. But for now all it does is integers.
Very easy to run it, and you will find these details on the project GitHub too, but I’ll re-iterate it here:
- Via maven
- Using the “executable” jar (
I prefer using maven but the option is out there for both.
This project also includes an utility class to read the output generated by this project into an array of int. Simply instantiate the class
liv.randomfile.IntReader and pass the file name in, then call
More details and the code on the project GitHub: https://github.com/liviutudor/RandomIntFile
More from my site