This week's book giveaway is in the Cloud/Virtualization forum. We're giving away four copies of Mastering Corda: Blockchain for Java Developers and have Jamiel Sheikh on-line! See this thread for details.
I am new to Java i have a assignment and that is i need to generate 1,00,00,000 (one crore ) random numbers and which should be seed based random numbers.
I have tried to write the program using java.util.Random; package and also using the setSeed method. But here i am getting duplicate numbers. For that i am first generating the random number and making the generated number as a key for a hashTable, So the hash keys will not be duplicates.
But here the problem is I am able to generate till 10 Lacs Numbers. If i try to generate more 1 Crore number I am getting the ERROR:
Exception in thread "main" java.lang.OutOfMemoryError: Java heap space
at java.util.Hashtable.rehash(Unknown Source)
at java.util.Hashtable.put(Unknown Source)
Please can any one help me. about this OR tell me which is the better way to generate unique random numbers in huge;
They are not truly random numbers; it is called something like "random selection from a declining population."
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.
You can alter the heap size with the Xmx and Xms options, which you can find here.
Thanks for you quick resonse: But here i need to generate the numbers which should be seed based. If some one wants to generate same random numbers we need to just change the seedNumber and has to generate:
and here code goes like this:
I should be able to generate the 1 crore random numbers: can you please give me the more details or any link which will help me to generate the truly random numbers. I am new to java just trying to lean...
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".