Re: Selecting a "random" quotation
- From: Tom Anderson <twic@xxxxxxxxxxxxxxx>
- Date: Thu, 12 Jun 2008 00:12:32 +0100
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)
final String[] candidates = category.getQuotations();2;
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.
.
- References:
- strange netbeans glitch not finding java classes
- From: gwlucas
- Selecting a "random" quotation
- From: Roedy Green
- strange netbeans glitch not finding java classes
- Prev by Date: Re: Looking for an intelligent tool to generate junit tests
- Next by Date: Re: Looking for an intelligent tool to generate junit tests
- Previous by thread: Re: Selecting a "random" quotation
- Next by thread: Re: Selecting a "random" quotation
- Index(es):
Relevant Pages
|