PHPBuilder - Generate a range of numbers

RSS Twitter

Generate a range of numbers

by: wall
October 24, 2001

Version: 1

Type: Function

Category: Algorithms

License: GNU General Public License

Description: Syntax: array steps(double start, double end, int steps) Returns an array of numbers, beginning with $start and gradually incrementing it so that the final number is $end. You decide how many numbers you want in the list, and the function will modify the incrementation accordingly. It will return a minimum of 2 munbers ($start and $end), no matter how small $steps is.


# Return a list with $steps elements, incrementing gradually from $start to $end.
# Example: steps(0,256,9) will return:
# array(0,32,64,96,128,160,192,224,256)
function steps($start, $end, $steps)
	settype($start, double);
	settype($end,   double);
	settype($steps, integer);

	# Avoid division by zero...
	if ($steps < 2)
		$steps = 2;

	# Initialise.
	$return = array();
	for ($i=0; $i<=$steps-1; $i++)
		# In step 2 of 10 this will be 0.2.
		$end_percent = $i / ($steps-1);
		# In step 2 of 10 this will be 0.8.
		$start_percent   = 1-$end_percent;
		# $start_percent + $end_percent will always be equal to 1.
		$return[] = $start*$start_percent + $end*$end_percent;

	return $return;


Comment and Contribute

Your comment has been submitted and is pending approval.




(Maximum characters: 1200). You have characters left.