• Post Reply Bookmark Topic Watch Topic
  • New Topic

Random records through Database or by using random numbers(Random class)  RSS feed

 
Kunal Lakhani
Ranch Hand
Posts: 622
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have random fetched records through database by 2 approaches:

1) By generating random numbers , and searching db as per the random numbers



2) By running a Query inside a Dao class



The second approach works faster than the first one. But i expected 1st approach to be much better.

What happens with the 1st approach is that controller goes to servlet, prints a line, and then doesn't do anything. But the server is running and its showing the bar at the bottom running very slowly. I am using MyEclipse8.5

I am trying to add the snapshot of error by pasting the url inside Img tag, buts its not happening. The file location is C:\Users\Kunal\Documents\errors.gif. How to do it?

 
Steve Luke
Bartender
Posts: 4181
22
IntelliJ IDE Java Python
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am trying to add the snapshot of error by pasting the url inside Img tag, buts its not happening. The file location is C:\Users\Kunal\Documents\errors.gif. How to do it?


How about you copy the error message into the post rather than try to show a picture. If you really want to show a picture with the error in it then the picture should be on the internet, not on your local computer.



I obviously can't help with your error since I don't know what it is, but I can point out a flaw in this code. You are staring with an empty list. You are generating random numbers between 0 and 5 - which is 0 inclusive, and 5 exclusive (see the API for java.util.Random). You only add a value to the List if the value isn't already in the list. Your while loop keeps going until the list has six values in it, but how can that happen when you only ever produce 5 integers (0,1,2,3,4)?

It seems to me that you really want to get a List with values 0,1,2,3,4,5 in it, but you want those 6 values in an arbitrary (random) order. What you should do is fill the List with those values then use java.util.Collections#shuffle(List).
 
Kunal Lakhani
Ranch Hand
Posts: 622
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Steve Luke. That's the problem with the code. It doesn't exits the while loop, so it doesn't runs further.

Thanks
 
Winston Gutkowski
Bartender
Posts: 10575
66
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Kunal Lakhani wrote:Thanks Steve Luke. That's the problem with the code. It doesn't exits the while loop, so it doesn't runs further.

And I hope you noticed what he said about shuffle(). That's definitely the way to go if you want to read all (or a portion of) the records in random sequence.
If you only want 1, why not use:
ListOfRandom.get( rand.nextInt(ListOfRandom.size()) );
?

Oh, and BTW:
  • ListOfRandom should be listOfRandom (see here).
  • You should put your rand factory in the servlet body. Creating a new one for every method call is rather wasteful.

  • HIH

    Winston
     
    Kunal Lakhani
    Ranch Hand
    Posts: 622
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Thanks Winston Gutkowski, i well look after shuffle(). Okay which approach is better, Query or Code (using shuffle)?
     
    • Post Reply Bookmark Topic Watch Topic
    • New Topic
    Boost this thread!