Campbell wrote:Try putting the numbers into a Set<Integer>; then duplicates will be eliminated quietly and you can use the return value from the add() method to verify insertion.
Yes, but note that he's already trying to use a Hashtable for this, and getting an OutOfMemoryError. A HashSet just uses aHashMap internally, which is pretty much equivalent to Hashtable in most respects, including memory usage. So while using a Hashtable here is less semantically clear, I think the real problem here is the memory usage. Which brings us to:
Campbell Ritchie wrote:You can alter the heap size with the Xmx and Xms options, which you can find here.
Shree, this is an important point, and I don't see any response from you. Did you try increasing the maximum memory using -Xmx? That may well be the easiest solution to your problem, if your machine has enough RAM.
A question: is there a requirement about the range of the random numbers? In the code above, you're using nextInt(30000000), generating values from 0 to 29999999. In your
later thread, you use nextLong(), generating values from -2^63 to 2^63 - 1. So, is there a specific requirement about the range, or are you just trying different things? Because if you can choose a smaller range, it's possible to solve this problem very efficiently using a BitSet or similar data structure to keep track of which numbers have previously been selected. That works if your range is something like 0 to 2 billion, or maybe 4 billion (with a little work). If you need a bigger range, well I suppose you could use a file-backed stand in for a BitSet, but that would be a lot slower. And if you need to cover the full range of a long, 2^64 different values, then it's very unlikely you have enough file space. Still, for a "small" range like 2-4 billion or less, this could work quite well.
Campbell wrote:You cannot get truly random numbers unless you do something like seeding a detector with radioactive substances
Which would of course be incompatible with the stated requirement that he use a seed, in order to be able to reproduce resuults. I think we can take it as a given that for this conversation, "random" should be interpreted as "pseudorandom".