Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Java - Flaw in random number generator

I am creating a program that should randomly appoint which problem I will be doing for my Java midterm. I created a program that runs 100,000 times and enters each question as a key in a hashmap, while its value is the number of counts generated out of 100,000. I have created the following simple program:

public class randomChoice {
    public static Map<Integer, Integer> dictionary = new HashMap<Integer, Integer>();

    public static void randInt() {
        Random rand = new Random();
        int randomNum = rand.nextInt((33 - 1) + 1) + 1;
        if (dictionary.containsKey(randomNum)) {
            dictionary.put(randomNum, dictionary.get(randomNum) + 1);
        } else {
            dictionary.put(randomNum, 0);
        }
    }

    public static void main(String[] args) {
        int i = 0;
        while (i < 100000) {
            randInt();
            i++;
        }
        System.out.println(dictionary);
        Map.Entry<Integer, Integer> maxEntry = null;

        for (Map.Entry<Integer, Integer> entry : dictionary.entrySet()) {
            if (maxEntry == null || entry.getValue().compareTo(maxEntry.getValue()) > 0) {
                maxEntry = entry;
            }
        }
        System.out.println("\nThe question I will be using for the midterm is " + maxEntry.getKey() + " with a total count of " + maxEntry.getValue());
        int total = 0;
        for (Map.Entry<Integer, Integer> entry : dictionary.entrySet()) {
            total = entry.getValue() + total;
        }
        System.out.println("\nTotal: " + total);
    }
}

My problem comes about when I try to add up all of the values of my HashMap (which is the frequency generated for each question out of 100,000). Here is the output that is generated, which prints the HashMap, the randomly picked question I will be doing that has the highest value, and lastly the total of all of the HashMap values:

> run randomChoice
{1=3038, 2=3025, 3=3009, 4=2945, 5=2996, 6=3049, 7=3004, 8=3078, 9=3011, 10=3012, 11=2995, 12=3041, 13=3116, 14=3015, 15=3029, 17=3058, 16=3141, 19=3045, 18=2976, 21=2988, 20=3065, 23=2943, 22=3106, 25=3025, 24=3093, 27=3092, 26=3058, 29=3018, 28=2981, 31=3035, 30=2970, 32=3007, 33=3003}

The question I will be using for the midterm is 16 with a total count of 3141

Total: 99967
> 

My question is, why is total coming out to 99967 instead of 100,000? Seems kind of fishy that it is EXACTLY 33 short and I have 33 questions to pick from. What am I doing wrong here? Where is my flaw which could help me create exactly 100,000 generated random numbers?


1 Answers

Your are exactly 33 short of 100,000 because when you encounter each one of your 33 map entries for the first time, you've placed a 0, not a 1. It's an off-by-one error.

Change

dictionary.put(randomNum, 0);

to

dictionary.put(randomNum, 1);
like image 199
rgettman Avatar answered Apr 23 '26 04:04

rgettman



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!