[thelist] PHP algorithm help

Andrew Seguin asegu at borg.darktech.org
Wed Sep 3 07:41:30 CDT 2003


Hello. I just typed the following code up, not tested or anything. I hope
mainly that it will give you a fair idea of how I picture a solution to
your problem using only math / rand().
There are probably syntax erros and etc, I'm sorry I don't have time to
truly debug... let me know if it helps.

Also be careful of lines wrapping around...


Andrew Seguin


------------------- CODE (PHP) ---------------------------------------------

//This will be part of looping structure for each row:
//Assumptions:
//    position 0 of each row is ignored.
//    position $row_length is valid.
//    this code will be integrated into a function to chose
//Notes:
//    This algo does not loop through valid positions. It does a few
calculations,
//    uses random number generator, etc... might be best to use looping
though for
//    small row lenghts (6? 7?) with small row seperations.

//////////////////////////////////////////////////////
//The following should be true for the first row.
$prev_row_pos = 0;

//////////////////////////////////////////////////////
//Set the following values per need.
$row_length = 20;
$min_seperation = 2;


//////////////////////////////////////////////////////
//trivial case of no limitations as to where to put on this row...
if ($prev_row_pos == 0) { $pos = rand(0, $row_length); return; }


//////////////////////////////////////////////////////
//step 1: where can the mark be put, if before the previous row's postion?
//        Find in terms of "last good position". If $pre == 3, then 1,2,3
are valid.
$pre = $prev_row_pos - $min_seperation;

if ($pre < 0)
	$pre = 0;


//////////////////////////////////////////////////////
//step 2: where can the mark be put, if after the previous row's position?
//        Find in terms of "first good positon". If $pre == 4, then
4,5,6,...$row_length are valid.
$post = $prev_row_pos + $min_seperation;

if ($post > $row_length)
	$post = $row_length + 1;


//////////////////////////////////////////////////////
//step 3: chances, probabilites... decide if before or after the previous
mark
$pre_chance = $pre / ($pre + $post);
$r = rand(0,1);
$pos = -1; //if still -1 at the end, then error. should be 0 for no position,
           //or in the range 1..$row_length if valid.


//////////////////////////////////////////////////////
// Chose at random whether or not before or after previous mark,
// based on equal probability distribution.
// handle also the case where there are no valid positions.
if ( ($pre == 0) && ($post == $row_length + 1) ) {
	//semi trivial case where there is no valid position on the row.
	$pos = 0;

} else {

	//did the electronic dice of the computer (rand) fall on post?
	if ($r > $pre_chance) {
		//use post.
		$pos = $rand(1, $pre)

	//did rand chose $pre?
	} else if ($r <= $pre_chance) {
		//use pre.
		$pos = $rand($post, $row_length)
	} else {
		//shouldn't get here if algo is sound.
		$pos = 0;
	}

}
------------------ / CODE (PHP) --------------------------------------------


More information about the thelist mailing list