Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- function normalverteilung( $max ){
- /*
- * @ see: http://de.wikipedia.org/wiki/Galtonbrett
- */
- $i = 0;
- $random = 0;
- $iteration = $max-1;
- for( $i=0; $i < $iteration; $i++ ){
- $random += mt_rand( 0, 1 );
- }
- return $random;
- }
- $e = 0;
- $iterations = 4711;
- $variance = 5;
- $result = array_fill( 0, $variance, 0 );
- for( $e=0; $iterations > $e; $e++ ){
- $result[ normalverteilung( $variance ) ]++;
- }
- /*
- * Display results
- */
- ksort( $result );
- $sum = array_sum( $result );
- $percent = $sum / 100;
- foreach( $result as $k=>$v ){
- $p = round( ( $v/$percent ), 2);
- $display_array[$k] = $p;
- }
- echo '<DOCTYPE html><html><head><title>Normalverteilung</title></head><body style="background-color:#666; color:#eee">';
- echo '<h2>Normalverteilung</h2>';
- printf( "Anzahl Zahlen: %d - Anzahl Durchläufe: %d<hr />", count( $result ), $sum );
- $c = 0;
- $d = 'f';
- foreach( $display_array as $raw ){
- echo '<div><p style="float:left">'.sprintf( "%02d", ($c+1) ).' </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" />';
- $c++;
- if($c>16)
- $d= 'ff';
- }
- echo '</body></html>';
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement