This week's giveaway is in the Testing forum. We're giving away four copies of TDD for a Shopping Website LiveProject and have Steven Solomon on-line! See this thread for details.
Hi All,
I want to generate unique random number from(0 to n number) such that if i run the program everytime it should give me a unique random number all the time.numbers should not repeated at any cost.
You can do it by using Math.random method. You have to maintain a list of all the numbers you have generated and used. Before returning the random number you have to check that it is already present in the list or not? If it is in list then you have to generate the function again.
I think you are supposed to call this random sampling from a declining population, or something similar. Please do a search of this forum; similar questions are asked about 6-10 times a year, and there will probably be helpful suggestions in the old threads.
Maneesh Godbole wrote:A Set would be more suitable instead of a list.
For what HG suggested, yes. However there's another algorithm using a List and Collections.shuffle(); this works quite well as long as N is not too large.