Interview Questions

Java - Random Number Generator

The following class contains utility methods to generate random numbers. The code is self explanatory with necessary comments.

RandomNumberGenerator.java

/**
 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS''
 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS
 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
 * POSSIBILITY OF SUCH DAMAGE.
 */

public class RandomNumberGenerator
{

        /**
         * This method returns a random number between 0 and maxLimit.
         */

        public int getRandomNumber(int maxLimit)
        {
                return (int) (Math.random() * maxLimit);
        }

        /**
         * This method returns a random number between smallest n digit number and largest n digit number. For example if
         * n=4, then a random number would be generated between 1000 and 9999.
         */

        public int getNDigitRandomNumber(int n)
        {
                int upperLimit = getMaxNDigitNumber(n);
                int lowerLimit = getMinNDigitNumber(n);
                int s = getRandomNumber(upperLimit);
                if (s < lowerLimit)
                {
                        s += lowerLimit;
                }
                return s;
        }

        /**
         * This method returns a random number between lowerLimit and upperLimit.
         */

        public int getRandomNumber(int lowerLimit, int upperLimit)
        {
                int v = (int) (Math.random() * upperLimit);
                if (v < lowerLimit)
                {
                        v += lowerLimit;
                }
                return v;
        }

        /**
         * This method returns a largest n digit number. The value returned depends on integer limit.
         */

        private int getMaxNDigitNumber(int n)
        {
                int s = 0;
                int j = 10;
                for (int i = 0; i < n; i++)
                {
                        int m = 9;
                        s = (s * j) + m;
                }
                return s;
        }

        /**
         * This method returns a lowest n digit number. The value returned depends on integer limit.
         */

        private int getMinNDigitNumber(int n)
        {
                int s = 0;
                int j = 10;
                for (int i = 0; i < n - 1; i++)
                {
                        int m = 9;
                        s = (s * j) + m;
                }
                return s + 1;
        }

        public static void main(String[] args)
        {
                RandomNumberGenerator rg = new RandomNumberGenerator();
                System.out.println("Random number between 10 and 20 -> " + rg.getRandomNumber(10, 20));
                System.out.println("Random number less than 8000 -> " + rg.getRandomNumber(8000));
                System.out.println("Random number with 4 digits -> " + rg.getNDigitRandomNumber(3));
        }
}

Output:

Random number between 10 and 20 -> 14
Random number less than 8000 -> 4486
Random number with 4 digits -> 7078