0

I am creating a method where if you pass in a parameter of type Random, then it will return a random object. Here is basically what I am trying to do:

public T choose(Random r) {
    int randomInt = r.nextInt(randomList.size()); // randomList is just a instance variable
    return randomList.get(randomInt);   
}

The random list has this the following strings:[2, 2, 2, 1, 1, 1, 1, c, c, c, a, a, a, a]

Then I made a driver with the following code

 for (int i = 0; i < 10; i++) {
        System.out.print(rndList.choose(rnd)); // rnd is initialized as a static Random variable
    }

However my outputs are not coming out random. I used the debugger and found out that my choose method generates an integer that is relatively low, so it will always print out either 2's or 1's but never c's or a's. I can't figure out why this is happening and help would be greatly appreciated.

EDIT: Problem was solved. I left out alot of detail, but when I called the size() method, that was something I overwrote which had an error which would return a smaller number than I would have liked. Thanks to dtech for noticing my silly mistake. Thank you for everyone who tried to help me!

7
  • 2
    Have you tried running it 50 times for example? Commented Mar 6, 2011 at 16:10
  • 2
    Also, how are you constructing your random number generator? With Random() or Random(someConstantIntegerValue)? If you use the latter, beware that you'll get the same "random" sequence each time you run the program. Commented Mar 6, 2011 at 16:14
  • Could you create a complete runnable test program? It looks like your error is outside the code you posted here. Commented Mar 6, 2011 at 16:17
  • When I run this code I get the output a1c211a121 what do you get and what do you expect to get? Commented Mar 6, 2011 at 16:21
  • 1
    The list in the first code example is named "randomList", in the second it's called "rndList". Just a typo? Commented Mar 6, 2011 at 16:35

3 Answers 3

4

At first glance nothing seems wrong with the code, so it might just be a random result. But your "Print it and check" method is very unreliable. Just use something like this:

final int N = 10000; // test 10.000 times
HashTable<Object, Integer> count = new HashTable(N);
for(int i=0;i < N;i++){
    Object o = rndList.choose(rnd);
    count.put(o, (count.get(o)==null?0:count.get(o))+1);
}
for(Map.Entry<Object, Integer> map : count.entrySet()){
    System.out.println(String.format("%s: %d", map.getKey().toString(), map.getValue()));
}

This will print on average something like: 2: 1429 1: 2857 c: 2143 a: 2857

Only if the numbers differ creatly you should be concerned.

Also make sure that you use the new Random() constructor, not new Random(somenumber). If you use the latter you will get the same number sequence every time.

Sign up to request clarification or add additional context in comments.

3 Comments

I put your method in my driver and I keep getting 2 being around 7500 and 1 being around 2500, and no mention of a or c. I have no idea whats happening. In my driver I make a static variable Random as the following: static Random rnd = new Random(); I have no idea what is going on!
Isn't the error on your randomList.size() then? those number seem to be from a randomList.size() that returns 4, which is the number of elements in your List. Aren't you using a method that returns the number of unique elements in the Set/List? Aren't you using a Set class instead of a List class?
wow you solved my problem haha. I know I left out alot of things in my code, but I was writing an abstraction of a map, and I used the size of the map which is actually smaller then the ArrayList.
1

send you random initialization code, are you getting exactly the same results each time? are you using a seed to create the Random object?

Comments

1

Here is the code I used. You need to provide more code to see why yours doesn't work.

public class Main<T> {
    private List<T> randomList = new ArrayList<T>();

    public  T choose(Random r) {
        int randomInt = r.nextInt(randomList.size()); // randomList is just a instance variable
        return randomList.get(randomInt);
    }


    public static void main(String... args) throws IOException, InterruptedException, ExecutionException {
        Main<String> rndList = new Main<String>();
        rndList.randomList.addAll(Arrays.asList("2, 2, 2, 1, 1, 1, 1, c, c, c, a, a, a, a".split(", ")));

        Random rnd = new Random();
        for (int i = 0; i < 10; i++) {
               System.out.print(rndList.choose(rnd)); // rnd is initialized as a static Random variable
           }

    }
}

prints

1ca1caa1a2

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.