Modified random distribution: Difference between revisions

m
→‎{{header|UNIX Shell}}: change loop style
m (→‎{{header|R}}: Syntax highlighting.)
m (→‎{{header|UNIX Shell}}: change loop style)
(19 intermediate revisions by 9 users not shown)
Line 26:
{{trans|Python}}
 
<langsyntaxhighlight lang="11l">F modifier(Float x) -> Float
R I x < 0.5 {2 * (.5 - x)} E 2 * (x - .5)
 
Line 49:
V delta = I last == N {‘N/A’} E String(count - last)
print(‘ #2.2, #4, #4: ’.format(Float(b) / bins, count, delta)‘’(‘#’ * Int(40 * count / mx)))
last = count</langsyntaxhighlight>
 
{{out}}
Line 73:
 
=={{header|Ada}}==
<langsyntaxhighlight Adalang="ada">with Ada.Text_Io;
with Ada.Numerics.Float_Random;
with Ada.Strings.Fixed;
Line 187:
New_Line;
end loop;
end Modified_Distribution;</langsyntaxhighlight>
{{out}}
<pre>Range Observations: 20000 Buckets: 25
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++}}==
<syntaxhighlight lang="c++">
#include <cmath>
#include <cstdint>
#include <functional>
#include <iomanip>
#include <iostream>
#include <random>
#include <string>
#include <vector>
 
std::random_device random;
std::mt19937 generator(random());
std::uniform_real_distribution<double> distribution(0.0F, 1.0F);
 
double modifier(const double& x) {
return ( x < 0.5 ) ? 2 * ( 0.5 - x ) : 2 * ( x - 0.5 );
}
 
double modified_random(const std::function<double(double)>& modify) {
double result = -1.0;
 
while ( result < 0.0 ) {
double random_one = distribution(generator);
double random_two = distribution(generator);
if ( random_two < modify(random_one) ) {
result = random_one;
}
}
return result;
}
 
int main() {
const int32_t sample_size = 100'000;
const int32_t bin_count = 20;
const double bin_size = 1.0 / bin_count;
 
std::vector<int32_t> bins(bin_count, 0);
 
for ( int32_t i = 0; i < sample_size; ++i ) {
double random = modified_random(modifier);
int32_t bin_number = floor(random / bin_size);
bins[bin_number]++;
}
 
std::cout << "Modified random distribution with " << sample_size << " samples in range [0, 1):"
<< std::endl << std::endl;
std::cout << " Range Number of samples within range" << std::endl;
 
const int32_t scale_factor = 125;
for ( float i = 0.0; i < bin_count; ++i ) {
std::string histogram = " " + std::string(bins[i] / scale_factor, '#') + " ";
std::cout << std::fixed << std::setw(4)<< std::setprecision(2) << i / bin_count << " ..< "
<< std::setw(4) << ( i + 1.0 ) / bin_count << histogram << bins[i] << std::endl;
}
}
</syntaxhighlight>
{{ out }}
<pre>
Modified random distribution with 100000 samples in range [0, 1):
 
Range Number of samples within range
0.00 ..< 0.05 ############################################################################ 9581
0.05 ..< 0.10 ##################################################################### 8662
0.10 ..< 0.15 ############################################################ 7516
0.15 ..< 0.20 ################################################### 6405
0.20 ..< 0.25 ############################################ 5595
0.25 ..< 0.30 #################################### 4502
0.30 ..< 0.35 ########################### 3464
0.35 ..< 0.40 ################### 2466
0.40 ..< 0.45 ############ 1525
0.45 ..< 0.50 #### 508
0.50 ..< 0.55 ### 482
0.55 ..< 0.60 ########### 1479
0.60 ..< 0.65 ################### 2494
0.65 ..< 0.70 ########################### 3440
0.70 ..< 0.75 ##################################### 4656
0.75 ..< 0.80 ############################################ 5544
0.80 ..< 0.85 ################################################### 6483
0.85 ..< 0.90 ########################################################### 7411
0.90 ..< 0.95 ################################################################### 8389
0.95 ..< 1.00 ########################################################################### 9398
</pre>
 
=={{header|Factor}}==
{{works with|Factor|0.99 2021-02-05}}
<langsyntaxhighlight lang="factor">USING: assocs assocs.extras formatting io kernel math
math.functions math.statistics random sequences
tools.memory.private ;
Line 249 ⟶ 408:
 
"Modified random distribution of values in [0, 1):" print nl
100,000 [ modifier ] 20 data .histogram</langsyntaxhighlight>
{{out}}
<pre>
Line 278 ⟶ 437:
 
=={{header|FreeBASIC}}==
<langsyntaxhighlight lang="freebasic">#define NRUNS 100000
#define NBINS 20
 
Line 320 ⟶ 479:
for b = 0 to NBINS-1
print using "Bin ## (#.## to #.##): & ####";b;b*db;(b+1)*db;histo(bins(b));bins(b)
next b</langsyntaxhighlight>
{{out}}
<pre>
Line 347 ⟶ 506:
=={{header|Fōrmulæ}}==
 
{{FormulaeEntry|page=https://formulae.org/?script=examples/Modified_random_distribution}}
Fōrmulæ programs are not textual, visualization/edition of programs is done showing/manipulating structures but not text. Moreover, there can be multiple visual representations of the same program. Even though it is possible to have textual representation &mdash;i.e. XML, JSON&mdash; they are intended for storage and transfer purposes more than visualization and edition.
 
'''Solution'''
Programs in Fōrmulæ are created/edited online in its [https://formulae.org website], However they run on execution servers. By default remote servers are used, but they are limited in memory and processing power, since they are intended for demonstration and casual use. A local server can be downloaded and installed, it has no limitations (it runs in your own computer). Because of that, example programs can be fully visualized and edited, but some of them will not run if they require a moderate or heavy computation/memory resources, and no local server is being used.
 
The following is the modifier random distribution function. It does not contain the modifier function, it is passed as a lambda expression:
In '''[https://formulae.org/?example=Modified_random_distribution this]''' page you can see the program(s) related to this task and their results.
 
[[File:Fōrmulæ - Modified random distribution 01.png]]
 
The example modifier is the following:
 
[[File:Fōrmulæ - Modified random distribution 02.png]]
 
The following functions groups a list of numbers in the given number of bins, producing the data necessary for a histogram.
 
[[File:Fōrmulæ - Modified random distribution 03.png]]
 
[[File:Fōrmulæ - Modified random distribution 04.png]]
 
[[File:Fōrmulæ - Modified random distribution 05.png]]
 
'''Test case 1. Showing the histogram of 50,000 numbers in 5 bins'''
 
[[File:Fōrmulæ - Modified random distribution 06.png]]
 
[[File:Fōrmulæ - Modified random distribution 07.png]]
 
'''Test case 2. Showing the histogram of 50,000 numbers in 20 bins'''
 
[[File:Fōrmulæ - Modified random distribution 08.png]]
 
[[File:Fōrmulæ - Modified random distribution 09.png]]
 
'''Test case 3. Showing the histogram of 50,000 numbers in 100 bins'''
 
[[File:Fōrmulæ - Modified random distribution 10.png]]
 
[[File:Fōrmulæ - Modified random distribution 11.png]]
 
=={{header|Go}}==
{{trans|Wren}}
<langsyntaxhighlight lang="go">package main
 
import (
Line 419 ⟶ 610:
fmt.Printf("%4.2f ..< %4.2f %s %s\n", binSize*fi, binSize*(fi+1), hist, commatize(bins[i]))
}
}</langsyntaxhighlight>
 
{{out}}
Line 453 ⟶ 644:
The modifier is a pure function which consumes a sequence of numbers (probably, random) and applies a modification rule to it.
 
<langsyntaxhighlight lang="haskell">import System.Random
import Data.List
import Text.Printf
Line 473 ⟶ 664:
showHist h = foldMap mkLine h
where
mkLine (b,n) = printf "%.2f\t%s %d%%\n" b (replicate n '▇') n</langsyntaxhighlight>
 
<pre>λ> showHist $ hist 0.05 $ take 50000 $ modify vShape $ randoms $ mkStdGen 1234
Line 496 ⟶ 687:
0.90 ▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇ 25%
0.95 ▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇ 29%</pre>
 
=={{header|J}}==
 
Implementation:
 
<syntaxhighlight lang="j">probmod=: {{y
while.do.r=.?0 0
if. (<u)/r do. {:r return.end.
end.
}}
 
mod=: {{|1-2*y}}</syntaxhighlight>
 
Task example:
 
<syntaxhighlight lang="j"> rnd=: mod probmod"0 i.1e4
 
bins=: 17%~i.18 NB. upper bounds (0 does not appear in result)
(":,.' ',.'#'#"0~0.06 <.@* {:@|:)/:~(~.,.#/.~) bins{~bins I. rnd
0.0588235 1128 ###################################################################
0.117647 977 ##########################################################
0.176471 843 ##################################################
0.235294 670 ########################################
0.294118 563 #################################
0.352941 423 #########################
0.411765 260 ###############
0.470588 125 #######
0.529412 27 #
0.588235 129 #######
0.647059 280 ################
0.705882 408 ########################
0.764706 559 #################################
0.823529 628 #####################################
0.882353 854 ###################################################
0.941176 996 ###########################################################
1 1130 ###################################################################</syntaxhighlight>
 
=={{header|Java}}==
<syntaxhighlight lang="java">
import java.util.List;
import java.util.concurrent.ThreadLocalRandom;
import java.util.stream.Collectors;
import java.util.stream.Stream;
 
interface ModifierInterface {
double modifier(double aDouble);
}
 
public final class ModifiedRandomDistribution222 {
 
public static void main(String[] aArgs) {
final int sampleSize = 100_000;
final int binCount = 20;
final double binSize = 1.0 / binCount;
List<Integer> bins = Stream.generate( () -> 0 ).limit(binCount).collect(Collectors.toList());
for ( int i = 0; i < sampleSize; i++ ) {
double random = modifiedRandom(modifier);
int binNumber = (int) Math.floor(random / binSize);
bins.set(binNumber, bins.get(binNumber) + 1);
}
System.out.println("Modified random distribution with " + sampleSize + " samples in range [0, 1):");
System.out.println();
System.out.println(" Range Number of samples within range");
final int scaleFactor = 125;
for ( int i = 0; i < binCount; i++ ) {
String histogram = String.valueOf("#").repeat(bins.get(i) / scaleFactor);
System.out.println(String.format("%4.2f ..< %4.2f %s %s",
(float) i / binCount, (float) ( i + 1.0 ) / binCount, histogram, bins.get(i)));
}
}
private static double modifiedRandom(ModifierInterface aModifier) {
double result = -1.0;
while ( result < 0.0 ) {
double randomOne = RANDOM.nextDouble();
double randomTwo = RANDOM.nextDouble();
if ( randomTwo < aModifier.modifier(randomOne) ) {
result = randomOne;
}
}
return result;
}
private static ModifierInterface modifier = aX -> ( aX < 0.5 ) ? 2 * ( 0.5 - aX ) : 2 * ( aX - 0.5 );
private static final ThreadLocalRandom RANDOM = ThreadLocalRandom.current();
 
}
</syntaxhighlight>
{{ out }}
<pre>
Modified random distribution with 100000 samples in range [0, 1):
 
Range Number of samples within range
0.00 ..< 0.05 ############################################################################ 9545
0.05 ..< 0.10 ################################################################### 8457
0.10 ..< 0.15 ############################################################ 7519
0.15 ..< 0.20 #################################################### 6513
0.20 ..< 0.25 ############################################ 5515
0.25 ..< 0.30 ################################### 4412
0.30 ..< 0.35 ############################ 3545
0.35 ..< 0.40 #################### 2507
0.40 ..< 0.45 ########### 1497
0.45 ..< 0.50 ### 475
0.50 ..< 0.55 #### 500
0.55 ..< 0.60 ########### 1496
0.60 ..< 0.65 #################### 2511
0.65 ..< 0.70 ############################ 3539
0.70 ..< 0.75 ################################### 4444
0.75 ..< 0.80 ########################################### 5494
0.80 ..< 0.85 ################################################### 6429
0.85 ..< 0.90 ############################################################ 7571
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>
 
=={{header|Julia}}==
 
<syntaxhighlight lang="text">using UnicodePlots
 
modifier(x) = (y = 2x - 1; y < 0 ? -y : y)
modrands(rands1, rands2) = [x for (i, x) in enumerate(rands1) if rands2[i] < modifier(x)]
histogram(modrands(rand(50000), rand(50000)), nbins = 20)
</langsyntaxhighlight>{{out}}
<pre>
 
Line 534 ⟶ 907:
 
=={{header|Mathematica}}/{{header|Wolfram Language}}==
<langsyntaxhighlight Mathematicalang="mathematica">ClearAll[Modifier, CreateRandomNumber]
Modifier[x_] := If[x < 0.5, 2 (0.5 - x), 2 (x - 0.5)]
CreateRandomNumber[] := Module[{r1, r2, done = True},
Line 548 ⟶ 921:
numbers = Table[CreateRandomNumber[], 100000];
{bins, counts} = HistogramList[numbers, {0, 1, 0.05}, "PDF"];
Grid[MapThread[{#1, " - ", StringJoin@ConstantArray["X", Round[20 #2]]} &, {Partition[bins, 2, 1], counts}], Alignment -> Left]</langsyntaxhighlight>
{{out}}
<pre>{0.,0.05} - XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
Line 573 ⟶ 946:
=={{header|Nim}}==
{{trans|Wren}}
<langsyntaxhighlight Nimlang="nim">import random, strformat, strutils, sugar
 
type ValRange = range[0.0..1.0]
Line 606 ⟶ 979:
for i in 0..<NumBins:
let hist = repeat(HistChar, (bins[i] / HistCharSize).toInt)
echo &"{BinSize * float(i):4.2f} ..< {BinSize * float(i + 1):4.2f} {hist} {bins[i]}"</langsyntaxhighlight>
 
{{out}}
Line 635 ⟶ 1,008:
{{trans|Raku}}
Uses any supplied distribution function, but defaults to uniform otherwise.
<langsyntaxhighlight lang="perl">use strict;
use warnings;
use List::Util 'max';
Line 669 ⟶ 1,042:
 
my @samples = distribution( function => \&modifier_notch, sample_size => 50_000);
print_histogram( data => \@samples, n_bins => 20, width => 64);</langsyntaxhighlight>
{{out}}
<pre>Bin Counts Histogram
Line 706 ⟶ 1,079:
 
=={{header|Phix}}==
<langsyntaxhighlight Phixlang="phix">function rng(integer modifier)
while true do
atom r1 := rnd()
Line 737 ⟶ 1,110:
sequence hist := repeat('#', round(bins[i]/HIST_CHAR_SIZE))
printf(1,"%s %s %,d\n", {LBLS[i], hist, bins[i]})
end for</langsyntaxhighlight>
{{out}}
<pre>
Line 766 ⟶ 1,139:
{{libheader|Phix/pGUI}}
A simple graphical plot. Note the labels are on the X-axis, so it's <code>v</code>-shaped, not <code><</code>-shaped: IupPlot does not support putting user-supplied labels on the Y-axis.
<langsyntaxhighlight Phixlang="phix">include pGUI.e
IupOpen()
Ihandle plot = IupPlot("GRID=YES, AXS_YAUTOMIN=NO")
Line 778 ⟶ 1,151:
IupShow(IupDialog(plot, `TITLE=Histogram, RASTERSIZE=1300x850`))
IupMainLoop()
IupClose()</langsyntaxhighlight>
 
=={{header|Python}}==
<langsyntaxhighlight lang="python">import random
from typing import List, Callable, Optional
 
Line 843 ⟶ 1,216:
print(f" {b / bins:5.2f}, {count:4}, {delta:>4}: "
f"{'#' * int(40 * count / mx)}")
last = count</langsyntaxhighlight>
 
{{out}}
Line 865 ⟶ 1,238:
=={{header|R}}==
Although it may not be immediately obvious, both modifier and gen are equivalent to the corresponding functions in the task.
<langsyntaxhighlight lang="rsplus">library(NostalgiR) #For the textual histogram.
modifier <- function(x) 2*abs(x - 0.5)
gen <- function()
Line 876 ⟶ 1,249:
}
data <- replicate(100000, gen())
NostalgiR::nos.hist(data, breaks = 20, pch = "#")</langsyntaxhighlight>
{{out}}
<pre>> NostalgiR::nos.hist(data, breaks = 20, pch = "#")
Line 914 ⟶ 1,287:
=={{header|REXX}}==
If a vertical histograph &nbsp; (instead of a &nbsp; <big><big><big>&lt;</big></big></big> &nbsp; shaped horizontal histograph) &nbsp; were to be used, &nbsp; it would be a &nbsp; <big><big>V</big></big> &nbsp; shaped.
<langsyntaxhighlight lang="rexx">/*REXX program generates a "<" shaped probability of number generation using a modifier.*/
parse arg randn bins seed . /*obtain optional argument from the CL.*/
if randN=='' | randN=="," then randN= 100000 /*Not specified? Then use the default.*/
Line 946 ⟶ 1,319:
#= # + 1; @.#= r /*bump counter; assign the MRD to array*/
end /*until*/
return</langsyntaxhighlight>
{{out|output|text=&nbsp; when using the default inputs:}}
<pre>
Line 974 ⟶ 1,347:
 
=={{header|Raku}}==
<syntaxhighlight lang="raku" perl6line>sub modified_random_distribution ( Code $modifier --> Seq ) {
return lazy gather loop {
my ( $r1, $r2 ) = rand, rand;
Line 994 ⟶ 1,367:
my @d = modified_random_distribution( &modifier );
 
print_histogram( @d.head(50_000), :n-bins(20), :width(64) );</langsyntaxhighlight>
{{out}}
<pre>
Line 1,019 ⟶ 1,392:
0.95, 4758: ■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■
</pre>
=={{header|Ruby}}==
<syntaxhighlight lang="ruby" line>def modifier(x) = (x - 0.5).abs * 2
 
def mod_rand
loop do
random1, random2 = rand, rand
return random1 if random2 < modifier(random1)
end
end
 
bins = 15
bin_size = 1.0/bins
h = {}
(0...bins).each{|b| h[b*bin_size] = 0}
 
tally = 50_000.times.map{ (mod_rand).div(bin_size) * bin_size}.tally(h)
m = tally.values.max/40
tally.each {|k,v| puts "%f...%f %s %d" % [k, k+bin_size, "*"*(v/m) , v] }
</syntaxhighlight>
{{out}}
<pre>0.000000...0.066667 **************************************** 6241
0.066667...0.133333 ********************************* 5286
0.133333...0.200000 *************************** 4365
0.200000...0.266667 ********************** 3576
0.266667...0.333333 ***************** 2724
0.333333...0.400000 *********** 1837
0.400000...0.466667 ***** 925
0.466667...0.533333 * 207
0.533333...0.600000 ***** 843
0.600000...0.666667 *********** 1761
0.666667...0.733333 ***************** 2699
0.733333...0.800000 *********************** 3629
0.800000...0.866667 **************************** 4514
0.866667...0.933333 ********************************* 5301
0.933333...1.000000 *************************************** 6092
</pre>
 
=={{header|Rust}}==
<syntaxhighlight lang="rust">use ndhistogram::{Histogram, ndhistogram, axis::Uniform};
use rand::Rng;
 
/// change x in [0.0, 1.0) to a split with minimum probability at 0.5
fn modifier(x: f64) -> f64 {
if x < 0.5 {
return 2.0 * (0.5 - &x);
} else {
return 2.0 * (&x - 0.5);
}
}
 
const WANTED: usize = 20_000;
 
fn main() {
let mut hist = ndhistogram!(Uniform::new(19, -0.0, 1.0));
let mut rng = rand::thread_rng();
for _ in 0.. WANTED {
loop {
let x: f64 = rng.gen::<f64>();
let y: f64 = rng.gen::<f64>();
if y < modifier(x) {
hist.fill(&f64::from(x));
break;
}
}
}
println!("{}", hist);
}
</syntaxhighlight>{{out}}
<pre>
VecHistogram1D(21 bins, sum=20000)
(-inf, -0.00) |
[0.00, 0.05) | #################################################
[0.05, 0.11) | ############################################
[0.11, 0.16) | #######################################
[0.16, 0.21) | ##################################
[0.21, 0.26) | ###########################
[0.26, 0.32) | ######################
[0.32, 0.37) | ################
[0.37, 0.42) | ###########
[0.42, 0.47) | #####
[0.47, 0.53) | #
[0.53, 0.58) | #####
[0.58, 0.63) | ###########
[0.63, 0.68) | ###############
[0.68, 0.74) | ######################
[0.74, 0.79) | ############################
[0.79, 0.84) | #################################
[0.84, 0.89) | ######################################
[0.89, 0.95) | ############################################
[0.95, 1.00) | ##################################################
[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}}
<langsyntaxhighlight ecmascriptlang="wren">import "random" for Random
import "./fmt" for Fmt
 
var rgen = Random.new()
Line 1,056 ⟶ 1,577:
var hist = HIST_CHAR * (bins[i] / HIST_CHAR_SIZE).round
Fmt.print("$4.2f ..< $4.2f $s $,d", binSize * i, binSize * (i + 1), hist, bins[i])
}</langsyntaxhighlight>
 
{{out}}
1,934

edits