kunal
Kunal Lakhani wrote:Hello
How to generate numbers without repeating the earlier generated number?
Rajat Jindal wrote:Its very easy.. you just need to set the seed for the java.util.Random class..
In case still you face any problem.. write back, I will give you a solution.
![]()
Vince Stout wrote:What you need to do is figure out a way to check each number generated against the previously generated numbers.
http://www.lifesbizzare.blogspot.com || OCJP:81%
Vince Stout wrote:What you need to do is figure out a way to check each number generated against the previously generated numbers.
There are only two hard things in computer science: cache invalidation, naming things, and off-by-one errors
kunal
Kunal Lakhani wrote:I did it using array, where i check each number generated against the previously generated numbers. This will not be efficient when having large volume of data
So, which approach should i go for?
There are only two hard things in computer science: cache invalidation, naming things, and off-by-one errors
Saurabh Pillai wrote:How about creating a vector for given range. Generate index using pseudo random generator. And then remove that number.
Vector? What’s wrong with ArrayList? Why is legacy code like Vector and StringTokenizer suggested so frequently?Saurabh Pillai wrote:How about creating a vector for given range. . . .
kunal
Campbell Ritchie wrote:
Vector? What’s wrong with ArrayList? Why is legacy code like Vector and StringTokenizer suggested so frequently?Saurabh Pillai wrote:How about creating a vector for given range. . . .
Kunal Lakhani wrote:Just have to fetch max 70 random numbers, out of 200-300. And there is no such speed requirements.
Kunal Lakhani wrote:
This will not be efficient when having large volume of data
Saurabh Pillai wrote:
I think the main benefit of my approach is to avoid the search for duplication.
kunal
Not at all. If we think you ought not to post a solution, we shall delete it. But your suggestion will not fulfil the original poster’s request.Rajat Jindal wrote:. . . but this I think I should post the solution for the problem.. . . .
Rajat Jindal wrote:Ok.. I will not post the solution
but the way discussion was going on ...was not going to provide a optimum solution...
![]()
Rajat Jindal wrote:what do you suggest ??
Campbell Ritchie wrote:I suggest you use a Set<Integer> or similar. Check whether your number is already an element of the Set.
Or I guess in my code setSeed() itself is not required.
Rajat Jindal wrote:I guess by using setSeed() method we cant remove repeated values and ultimate option is to use some kind of solution like Set<Integer>.
Can anyone confirms me whether setSeed() remove repetition of numbers or not?![]()
kunal
Kunal Lakhani wrote:This may be the right implementation.
Joanne
You also have a logic error - once you add the value zero to your Set you will never add any more numbers because your if condition will always be true.
kunal
Joanne
kunal