Selecting a "random" quotation
- From: Roedy Green <see_website@xxxxxxxxxxxxxxxxxxxx>
- Date: Wed, 11 Jun 2008 14:55:06 GMT
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 ];
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.
--
Roedy Green Canadian Mind Products
The Java Glossary
http://mindprod.com
.
- Follow-Ups:
- Re: Selecting a "random" quotation
- From: Tim Smith
- Re: Selecting a "random" quotation
- From: Tom Anderson
- Re: Selecting a "random" quotation
- From: John B. Matthews
- Re: Selecting a "random" quotation
- References:
- strange netbeans glitch not finding java classes
- From: gwlucas
- strange netbeans glitch not finding java classes
- Prev by Date: Re: why 31 in hashcode for string
- Next by Date: Re: why 31 in hashcode for string
- Previous by thread: Re: strange netbeans glitch not finding java classes
- Next by thread: Re: Selecting a "random" quotation
- Index(es):
Relevant Pages
|