Random code snipets

I’ve been working on some perty usless code that are more for fun than anything else, but oh well.

Feel free to post some of your own.

Here they are…

Find Pi


 unsigned double float pi;
  
 void main(void)
 	{
 	pi = 1;
 
 	for(count = 0, top = 2, bottem = 1; count < 10000; count++)
 		{
 		pi *= top/bottem;
 		bottem += 2;
 
 		pi *= top/bottem;
 		top += 2;
 		}
  
 	printf ("pi = %f", pi*2);
 	}
 

Random Signature


 <?php 
 
 header("Content-type: image/png");
 
 //read folder
 $folder = opendir(".");
 
 while ($file = readdir($folder))
 
 $names[count($names)] = $file;
 
 closedir($folder);
 
 //sort file names in array
 sort($names);
 
 //remove any non-images from array
 $tempvar = 0;
 
 for ($i = 0; $names$i]; $i++)
 	{
 	$ext=strtolower(substr($names$i],-4));
 
 if ($ext == ".png")
 	{
 	$names1$tempvar] = $names$i]; $tempvar++;}
 	}
 
 //random
 srand ((double) microtime() * 10000000);
 $rand_keys = array_rand ($names1, 2);
 
 //random image from array
 $im = imagecreatefrompng($names1$rand_keys[0]]);
 
 imagepng($im);
 ?>
 

Create a directory on your server and put this script there along with any png images. It will select one randomly and then output it to display image. This the exact code that I use for mine. (hit F5 a couple of times to watch it change)

Could you add these to the repository?

http://nrg.chaosnet.org/repository - You’ll need to register

Err … Could you not add these to the repository? Maybe I got the wrong idea, but I thought the repository was supposed to be a place to find useful and robot-related code (ideally to spark either inspiration or recognition ;)). These snippets are useless to any team looking for some code for their robot, and hence would only clutter up the repository – thereby making it a less useful place, as it would be harder to find what you’re looking for.

I mean, really, the poster did say:

I’ve been working on some perty usless code that are more for fun than anything else, but oh well.

Also, I saw in another post that you asked for the beacon/tracker code to be uploaded to the repository. I believe that this would be a mistake, too. This code can be got in other places (it is on the FIRST website, afterall). Anyone going to the repository should already have access to that code, and hence, again, the unnecessary posting of it would only add clutter, and make the repository less useful.

I realize you are excited about getting a lot of useful code uploaded to the repository … but more is not necessarily better. Especially if it is more uneccesary or useless code! Remember: quality, not quantity, is what counts … especially when programming. Perhaps I mistook the purpose of the repository … but it doesn’t seem to me like there is a need for a repository of random, un-related and un-useful pieces of code.

That’s a very poor formula for pi. It converges very, very, very slowly.

Try some of these links for some better formulas:
http://www.geocities.com/SiliconValley/Bay/9187/pi.htm
http://elephant.linux.net.cn/articles/pi.php?lang=en

Chudnovsky’s algorithm would be a fine choice. You get 14 digits every iteration of the algorithm.

New version of random image


 <?php
// Last Updated: Sunday, January 26, 2004 at 11:43:45 AM

// *****IPORTANT*****
// GIF support has been disabled due to complications
// of another nature.

/**************************************************/
/* Supported image types						  */
/**************************************************/
/* Input:										 */
/* PNG										    */
/* JPEG										   */
/* WBMP										   */
/* XBM										    */
/* XPM										    */
/*											    */
/* Output:									    */
/* PNG										    */
/* JPEG										   */
/* GIF (if supported, else defualts to PNG)	   */
/**************************************************/

/**************************************************/
/* -Options									   */
/**************************************************/
/* Image Output type							  */
/*											    */
/* Desription:								    */
/* Allows the ability to overide the type of	  */
/* image output by the program.				   */
/*											    */
/* Settings:									  */
/* auto, png, jpeg, and gif (if supported)		*/
/**************************************************/

$imageoutputtype = "auto";

/**************************************************/
/* JPEG Quality								   */
/*											    */
/* Description:								   */
/* Allows you to set the compression level of	 */
/* JPEGs generated by the program.				*/
/*											    */
/* Settings:									  */
/* defualt, [0-100] (0 worst and 100 best)		*/
/**************************************************/

$jpegquality = "defualt";

/**************************************************/
/* NO NEED TO CHANGE ANYTHING BELOW			   */
/**************************************************/

//does a check to see if GD is installed and running
/*
if (!isset(imagetypes()))
	{
	die("This server does not have image support.");
	}
*/

if(!isset($imageoutputtype))
	{
	$imageoutputtype = "auto";
	}

if(!$jpegquality >= 0 && !$jpegquality <= 100)
	{
	$jpegquality = 100;
	}

//read folder
$folder = opendir(".");

while ($file = readdir($folder))

$names[count($names)] = $file;

closedir($folder);

//sort file names in array
sort($names);

//remove any non-images from array
$tempvar = 0;

for ($i = 0; $names$i]; $i++)
	{
	$ext=strtolower(substr($names$i],-4));

	if ($ext == ".png" || $ext == ".jpg" || $ext == "jpeg" || $ext == "wbmp" || $ext == ".xbm" || $ext == ".xpm")
		{
		$names1$tempvar] = $names$i];
		$tempvar++;
		}
	}

//random
srand ((double) microtime() * 10000000);
$rand_keys = array_rand ($names1, 2);


//random image from array
$rimage = $names1$rand_keys[0]];


//image identification
$imagetype = strtolower(substr($rimage,-4));


//image import
if($imagetype == ".png")
	{
	$image = imagecreatefrompng($rimage);

	if($imageoutputtype == "auto")
		{
		$imageoutputtype = "png";
		}
	}

  elseif($imagetype == ".jpg" || $imagetype == "jpeg")
	{
	$image = imagecreatefromjpeg($rimage);

	if($imageoutputtype == "auto")
		{
		$imageoutputtype = "jpeg";
		}
	}

  elseif($imagetype == ".gif")
	{
	$image = imagecreatefromgif($rimage);

	if($imageoutputtype == "auto")
		{
		$imageoutputtype = "gif";
		}
	}

  elseif($imagetype == "wbmp")
	{
	$image = imagecreatefromwbmp($rimage);

	if($imageoutputtype == "auto")
		{
		$imageoutputtype = "png";
		}
	}

  elseif($imagetpye == ".xbm")
	{
	$image = imagecreatefromxbm($rimage);

	if($imageoutputtype == "auto")
		{
		$imageoutputtype = "png";
		}
	}

  elseif($imagetype == ".xpm")
	{
	$image = imagecreatefromxpm($rimage);

	if($imageoutputtype == "auto")
		{
		$imageoutputtype = "png";
		}
	}


//image output
if($imageoutputtype == "gif")
	{
	if(imagetypes() & IMG_GIF)
		{
		header("Content-type: image/gif");
		imagegif($image);
		}
	  else
		{
		header("Content-type: image/png");
		imagepng($image);
		}
	}

  elseif($imageoutputtype == "jpeg" || $imageoutputtype == "jpg")
	{
	if(imagetypes() & IMG_JPG)
		{
		header("Content-type: image/jpeg");
		imagejpeg($image, '', $jpegquality);
		}
	  else
		{
		header("Content-type: image/png");
		imagepng($image);
		}
	}

  elseif($imageoutputtype == "png")
	{
	header("Content-type: image/png");
	imagepng($image);
	}

  else
	{
	header("Content-type: image/png");
	imagepng($image);
	}
?>