Modified random distribution: Difference between revisions

m
→‎{{header|UNIX Shell}}: change loop style
m (→‎{{header|UNIX Shell}}: change loop style)
 
(4 intermediate revisions by 3 users not shown)
Line 215:
0.92..0.96 *******************************************************************
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++}}==
Line 732 ⟶ 807:
0.90 ..< 0.95 ################################################################### 8474
0.95 ..< 1.00 ############################################################################ 9557
</pre>
 
=={{header|javascript}}==
 
<syntaxhighlight lang=javascript>function modifier(x) { return (x < .5 ? -1 : +1)*(2*(x-.5)) }
 
function random(m) {
let random1, random2;
while (true) {
random1 = Math.random();
random2 = Math.random();
if (random2 < m(random1)) {
return random1;
}
}
}
 
const N = 10000;
const bins = 20;
var numbers = [];
for (i=0;i<N;i++) {
let number = random(modifier);
numbers.push(number);
}
 
const delta = 1.0/bins;
var count = 0;
for (ceil=delta; ceil<1.0+delta; ceil+=delta) {
for (n of numbers) {
if ((n < ceil) && (ceil - delta <= n)) {
count++;
}
}
let width = count/N * 80;
let bar = '';
 
for (i = 0; i<width; i++) bar+='#';
console.log(bar);
count = 0;
}</syntaxhighlight>
{{out}}
<pre>########
#######
#######
######
#####
####
###
###
##
#
#
##
###
###
####
#####
######
######
#######
########
</pre>
 
Line 1,348 ⟶ 1,484:
[1.00, inf) |
</pre>
 
=={{header|UNIX Shell}}==
{{works with|bash}}
<syntaxhighlight lang=bash># NOTE: In bash, RANDOM returns an integer from 0 to 32767 (2**15-1)
random() {
local m="$1"
local -i random1 random2
while true
do
random1=RANDOM
random2=RANDOM
if ((random2 < $("$m" $random1)))
then echo $random1; break
fi
done
}
 
modifier() {
local -i x=$1
echo $((x < 2**14 ? 2**14 - x : x - 2**14 ))
}
 
declare -i N=10000 bins=20
declare -a histogram
for ((i=0;i<N;i++))
do ((histogram[bins*$(random modifier)/2**15]++))
done
 
for ((i=0;i<bins;i++))
do
for ((j=0;j< ${histogram[i]-0}*bins*50/N;j++))
do echo -n '#'
done
echo
done</syntaxhighlight>
{{out}}
<pre>#################################################################################################
################################################################################
############################################################################
###################################################################
#######################################################
###############################################
###################################
##########################
#############
######
#####
###############
##########################
##################################
#############################################
#####################################################
##############################################################
#######################################################################
####################################################################################
################################################################################################</pre>
 
=={{header|Wren}}==
{{libheader|Wren-fmt}}
<syntaxhighlight lang="ecmascriptwren">import "random" for Random
import "./fmt" for Fmt
 
var rgen = Random.new()
1,934

edits