Advertisement
Guest User

Galton-Brett

a guest
Feb 22nd, 2011
231
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 1.29 KB | None | 0 0
  1. <?php
  2. function normalverteilung( $max ){
  3.     /*
  4.      * @ see: http://de.wikipedia.org/wiki/Galtonbrett
  5.      */
  6.      
  7.     $i = 0;
  8.     $random = 0;
  9.     $iteration = $max-1;
  10.    
  11.     for( $i=0; $i < $iteration; $i++ ){
  12.         $random += mt_rand( 0, 1 );
  13.     }
  14.    
  15.     return $random;
  16. }
  17.  
  18. $e = 0;
  19. $iterations = 4711;
  20. $variance = 5;
  21. $result = array_fill( 0, $variance, 0 );
  22.  
  23. for( $e=0; $iterations > $e; $e++ ){
  24.     $result[ normalverteilung( $variance ) ]++;
  25. }
  26.  
  27.  
  28. /*
  29.  * Display results
  30.  */
  31. ksort( $result );
  32.  
  33. $sum = array_sum( $result );   
  34. $percent = $sum / 100;
  35.  
  36. foreach( $result as $k=>$v ){
  37.     $p = round( ( $v/$percent ), 2);
  38.     $display_array[$k] = $p;
  39. }
  40.  
  41. echo '<DOCTYPE html><html><head><title>Normalverteilung</title></head><body style="background-color:#666; color:#eee">';
  42. echo '<h2>Normalverteilung</h2>';
  43. printf( "Anzahl Zahlen: %d - Anzahl Durchläufe: %d<hr />", count( $result ), $sum );
  44.  
  45. $c = 0;
  46. $d = 'f';
  47. foreach( $display_array as $raw ){
  48.     echo '<div><p style="float:left">'.sprintf( "%02d", ($c+1) ).'&nbsp;</p><div style="float:left;background-color:#'.dechex($c).dechex($c).$d.'; width:'.$raw.'em; border:1px solid black; height:2em"><p style="position:relative; bottom:0.2em; left:0.5em">'.$raw.'%</p></div></div><br style="clear:left" />';
  49.     $c++;
  50.         if($c>16)
  51.             $d= 'ff';
  52.        
  53. }
  54.  
  55. echo '</body></html>';
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement