My code is working fine but with one problem I can’t get exactly the number of documents that I specified I need to get 28 documents randomly but sometimes I get 23 or 26 or 30… Can someone tell me why? What’s wrong with my code?
As Carl mentioned, you are probably not getting 28 distinct random numbers. There is an off-by-one error as well, since
For x = 0 To 28
actually produces 29 iterations. You’ll need to create a list of random numbers first, making sure that there are 28 of them, all unique, then run through the list to get the corresponding documents. The easiest way to do that is using a List:
Then you can use a Forall on the list to get your documents.
This may not be the most efficient way of doing things, but it is the most readable and maintainable. Changing the range and the number of numbers chosen is very easy, and the time complexity (which should be O(log n)) means that things won’t get out of control slow as the number of choices increases (at least up to the point where new choices are more likely to be hits than misses).
[EDIT: an error in the code could have resulted in a 0 and would have made the last document in the view unreachable; adding one to the truncated random number fixes it.]