Let’s say I have 45 documents. For each document,
Form = “LotteryBall”
and each document has a unique value in a field called “ballvalue” between “1” and “45” (Text), inclusive.
Now, I want a view that, when viewed through a web-browser, will show a random selection of four of these documents (no more), with their values. For some reason, @Random doesn’t like being in a View Selection formula as it only picks the same document when I wrote this bad boy:
SELECT Form = “LotteryBall” & ballvalue = @Text(@Integer((46 - 1) * @Random + 1))
BTW, this was only for a single document instead of four. I want four.
@Random won’t even let me feed it a seed! (I don’t understand how any modern-day random function could not have a seed parameter.)
Anyone have any suggestions for this view? (View performance is not too important at this stage.)
Subject: RE: Tonight’s winning numbers are…
I believe what you’re seeing here is that the server doesn’t realize @Random will return a different value each time. Accordingly, it doesn’t re-evaluate to see whether a document should appear in this view unless the document has been modified. The view index is already built; it doesn’t need to look at any documents that were not modified later than the last index update. If you set the view index to discard after every use, you should get different results every time.
Of course, the @Random function is evaluated for each document as the index is being calculated. This does not mean you match exactly one document; that was just luck this one time. You might easily match no documents, or more than one.
I’m not offhand sure what the algorithm is for this function, but I suspect it seeds itself off the system clock the first time it’s called.
I suggest you use @Random in a sorted, hidden view column instead. In your $$ViewTemplate form, set it to display the first four rows:
Subject: RE: Tonight’s winning numbers are…
Andre, this is a brilliant solution - althought I can’t implement the part that involves embedding the view. The view I need to use is a regular, hidden view - the view gets looked up. So, I will rewrite the look-up to get the first four rows instead.
Will definitely implement the view-indexing solution. I vaguely thought it was something to do with a static index too, but I wasn’t sure. Thanks for telling me that it most likely is.
Subject: Tonight’s winning numbers are…
Well, I haven’t worked too much with web based dbs, but perhaps have a field in the docs that could have a Y or N value, then have a LS agent that picks 4 of the 45 docs at random, and sets the field to Y, and then sets that field to N for the other 41.
SELECT Form = “LotteryBall” and selectionfield = “Y”
Also, in LS you can seed the random number generator.
Probably best to have code set all docs to N first, then try to set the 4 to Y. Have code check to see if field is already Y, and keep in loop until it has set four of them to Y. This is in case you get any dupes on the first 4 tries for a random number.
Subject: Tonight’s winning numbers are…
If view performance is really NOT that importatnt… try these :
SELECT Form = “LotteryBall” & ballvalue = @Text(@Integer((46 - 1) * @Random + 1)) & @Now = @Now
Subject: RE: Tonight’s winning numbers are…
Holy mackarel that works!
Way to go Lotus! Making developers write crazy crap to overcome inherent system behaviour since 1987…