Modified random distribution: Difference between revisions
Content added Content deleted
(javascript entry) |
(Added Algol 68) |
||
Line 215: | Line 215: | ||
0.92..0.96 ******************************************************************* |
0.92..0.96 ******************************************************************* |
||
0.96..1.00 ************************************************************************</pre> |
0.96..1.00 ************************************************************************</pre> |
||
=={{header|ALGOL 68}}== |
|||
{{Trans|Wren|with slightly modified output}} |
|||
<syntaxhighlight lang="algol68"> |
|||
BEGIN # Modified random distribution - translation of the Wren sample # |
|||
next random; # initialise the random number generator # |
|||
PROC rng = ( PROC(REAL)REAL modifier )REAL: |
|||
BEGIN |
|||
REAL r1, r2; |
|||
WHILE |
|||
r1 := random; |
|||
r2 := random; |
|||
r2 >= modifier( r1 ) |
|||
DO SKIP OD; |
|||
r1 |
|||
END # rng # ; |
|||
PROC modifier = ( REAL x )REAL: 2 * ABS ( 0.5 - x ); |
|||
INT n = 100 000; |
|||
INT num bins = 21; |
|||
REAL bin size = 1 / num bins; |
|||
CHAR hist char = "#"; |
|||
INT hist char size = 125; |
|||
[ 0 : num bins - 1 ]INT bins ; FOR i FROM LWB bins TO UPB bins DO bins[ i ] := 0 OD; |
|||
FOR i FROM 0 TO n DO |
|||
bins[ ENTIER ( rng( modifier ) / bin size ) ] +:= 1 |
|||
OD; |
|||
PROC f2 = ( REAL v )STRING: # formatting routine # |
|||
BEGIN |
|||
STRING result := fixed( ABS v, 0, 2 ); |
|||
IF result[ LWB result ] = "." THEN "0" +=: result FI; |
|||
IF v < 0 THEN "-" +=: result FI; |
|||
result |
|||
END # FMT # ; |
|||
print( ( "Modified random distribution with ", whole( n, 0 ), " samples in range [0, 1):", newline ) ); |
|||
print( ( " Range Number of samples within that range", newline ) ); |
|||
FOR i FROM LWB bins TO UPB bins DO |
|||
STRING hist = hist char * ROUND ( bins[ i ] / hist char size ); |
|||
print( ( f2( bin size * i ), " ..< " ) ); |
|||
print( ( f2( bin size * ( i + 1 ) ), " ", whole( bins[ i ], -5 ), " ", hist, newline ) ) |
|||
OD |
|||
END |
|||
</syntaxhighlight> |
|||
{{out}} |
|||
<pre> |
|||
Modified random distribution with 100000 samples in range [0, 1): |
|||
Range Number of samples within that range |
|||
0.00 ..< 0.05 8990 ######################################################################## |
|||
0.05 ..< 0.10 8043 ################################################################ |
|||
0.10 ..< 0.14 7265 ########################################################## |
|||
0.14 ..< 0.19 6366 ################################################### |
|||
0.19 ..< 0.24 5442 ############################################ |
|||
0.24 ..< 0.29 4631 ##################################### |
|||
0.29 ..< 0.33 3805 ############################## |
|||
0.33 ..< 0.38 2780 ###################### |
|||
0.38 ..< 0.43 1725 ############## |
|||
0.43 ..< 0.48 907 ####### |
|||
0.48 ..< 0.52 236 ## |
|||
0.52 ..< 0.57 915 ####### |
|||
0.57 ..< 0.62 1778 ############## |
|||
0.62 ..< 0.67 2696 ###################### |
|||
0.67 ..< 0.71 3657 ############################# |
|||
0.71 ..< 0.76 4539 #################################### |
|||
0.76 ..< 0.81 5382 ########################################### |
|||
0.81 ..< 0.86 6337 ################################################### |
|||
0.86 ..< 0.90 7230 ########################################################## |
|||
0.90 ..< 0.95 8173 ################################################################# |
|||
0.95 ..< 1.00 9104 ######################################################################### |
|||
</pre> |
|||
=={{header|C++}}== |
=={{header|C++}}== |