Archive - Nov 27, 2006

Date
  • All
  • Jan
  • Feb
  • Mar
  • Apr
  • May
  • Jun
  • Jul
  • Aug
  • Sep
  • Oct
  • Nov
  • Dec

dp.SyntaxHighlighter

dp.SyntaxHighlighter is a javascript syntax highlighter from DreamProjections. Yahoo uses it on it's pages for developers to highlight syntax. 10Kb after compress made it a small burden for loading new pages. Some syntax highlighter in action is here:


Editor Comment:

I was going to use dp.SyntaxHighlighter, then I found it conflict with the CSS settings. That's how I chose GeSHi over it.

Contest for the fastest weighted Random script

Requirement:
1. Only PHP is allowed
2. It can be either a function or a class
3. the input array of string is a numbered array, each one stores a string. It's created by using:
$array[] = 'string';
4. The input array of weight is also a numbered array, for a $weight[$i], it resemble the weight value of $array[$i]. $weight contain only positive integers. $weight[$i] does not need to be created, if there is no $weight[$i] match $array[$i], the weight 1 will be used
5. Return one of the string of the $array dependent on the $weight
Note: in case you don't know what is weighted random. two or more items, each have a weight, weight decide the chance it get chosen. A item with weight 5 have 5 times more chance to be chosen than a item with weight 1, 2.5 times more chance than a item with weight 2. let me give you an example of what it is like:

$array[] = 'weight 1';
$array[] = 'weight 2, have higher chance to be chosen than weight 1';
$array[] = $array[1];
echo $array[array_rand($array)];

Winner Prize:
Get to put a link on this site... Free!

Final Test for speed:
There will be three test it have to go though, here are the inputs for each:
Test 1:

$array[] = 'test for speed of no weight arrays';
$array[] = 'how fast is this';
$array[] = 'the final result for this * 2';

Test 2:

$array[] = 'test for weight';
$weight[] = 30000;
$array[] = 'with HUGE numbers';
$weight[] = 2000;
$array[] = 'final result for this * 5';
$weight[] = 50000;

Test 3:

$array[]= 'test for weight array only on some';
$weight[] = 20000;
$array[]= 'final result for this * 3';

Winner is the person who have the smallest number after the weighted number of all tests, which is
Final result for test 1 *2 + Final result for test 2 * 5 + final result for test 3 * 3

Note: When would people need weighted random?
Most likely in games. Kill a monster, what will be dropped? Diamond weight 1 and wooden sword weight 1000000, then most likely you get the wooden sword.

Honey Pot that kill bots