Re: Selecting a "random" quotation



On Wed, 11 Jun 2008, Roedy Green wrote:

Here is some code I use to select a "random" quotation to put an a web
page. It changes every 4 hours.

/**
* 86,400,000 the number of milliseconds in 24 hour day. Easily
fits into an int.
* We change the quotations every 4 hours, UTC.
*/
private static final int CHANGE_INTERVAL_IN_MILLIS = 4 * 60 * 60 *
1000;

final Adler32 digester = new Adler32();
int seed = ( int ) ( System.currentTimeMillis() /
CHANGE_INTERVAL_IN_MILLIS );
/// digester.update(int) not clearly documented, probably
wants ubyte.
digester.update( ( byte ) ( seed & 0xff ) );
digester.update( ( byte ) ( ( seed >>> 8 ) & 0xff ) );
digester.update( ( byte ) ( ( seed >>> 16 ) & 0xff ) );
digester.update( ( byte ) ( ( seed >>> 24 ) & 0xff ) );
digester.update( fileBeingProcessed.getPath().getBytes(
"UTF-8" ) );

// get rid of high sign bit, and low bit, which seem to be
1 disproportionately.
final int digest = (( ( int ) digester.getValue() ) << 1)
2;
final String[] candidates = category.getQuotations();

final String chosenQuote = candidates[ digest %
candidates.length ];

Why are you doing all this? Why not just new Random(), generate and throw away a few ints, and then pick one out and use it?

However it often SEEMS to sometimes pick the same quote for two different pages, more often than I would intutitively think it would.

Further it SEEMS to favour certain quotes in the collection. I wonder
if this just my imagination, or there is some flaw in the algorithm.

I also have a random quote selector, and i also notice this. There are even times when i think it's picking quotes specifically tailored for the email i'm about to write. I think it's an illusion - if you randomly pick quotes, you don't expect them all to appear at the same frequency, but for some to, by chance, come up more often. Isn't it a binomial distribution or something? Anyway, it's exactly the kind of thing that the human brain interprets as a pattern.

tom

--
Hesgadin. It was in two parts - both of them silent. I remember this map
came with a letter accusing me of stealing eggs. I had never understood
the relationship of the map to the accusation. I still don't, but I'm
grateful for the map.
.



Relevant Pages

  • Re: Columbia University should not cancel
    ... Maybe since you seem to have a good source for Ahmadinejad ... quotes, you can find one where he says what he means... ... I, for instance, think Northern Ireland should be wiped off the map. ...
    (rec.sport.football.college)
  • Re: Columbia University should not cancel
    ... Maybe since you seem to have a good source for Ahmadinejad ... quotes, you can find one where he says what he means... ... I, for instance, think Northern Ireland should be wiped off the map. ...
    (rec.sport.football.college)
  • Re: Columbia University should not cancel
    ... Maybe since you seem to have a good source for Ahmadinejad ... quotes, you can find one where he says what he means... ... I, for instance, think Northern Ireland should be wiped off the map. ...
    (rec.sport.football.college)
  • Re: federer ... thinking along the likes of whisper :-)
    ... Vari L. Cinicke wrote: ... > Whisper is all over the map, and there are quotes of every description ...
    (rec.sport.tennis)
  • Re: [RFC] Re: Parsing kernel parameters and escaping "
    ... Attempt to handle quotes in kernel parameters ... There was a limitation for kernel parameters with regards to quoting. ... +/* modifies args with handled quotes ... +int printf; ...
    (Linux-Kernel)