Modified random distribution: Difference between revisions
m
syntax highlighting fixup automation
(J) |
Thundergnat (talk | contribs) m (syntax highlighting fixup automation) |
||
Line 26:
{{trans|Python}}
<
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</
{{out}}
Line 73:
=={{header|Ada}}==
<
with Ada.Numerics.Float_Random;
with Ada.Strings.Fixed;
Line 187:
New_Line;
end loop;
end Modified_Distribution;</
{{out}}
<pre>Range Observations: 20000 Buckets: 25
Line 218:
=={{header|Factor}}==
{{works with|Factor|0.99 2021-02-05}}
<
math.functions math.statistics random sequences
tools.memory.private ;
Line 249:
"Modified random distribution of values in [0, 1):" print nl
100,000 [ modifier ] 20 data .histogram</
{{out}}
<pre>
Line 278:
=={{header|FreeBASIC}}==
<
#define NBINS 20
Line 320:
for b = 0 to NBINS-1
print using "Bin ## (#.## to #.##): & ####";b;b*db;(b+1)*db;histo(bins(b));bins(b)
next b</
{{out}}
<pre>
Line 355:
=={{header|Go}}==
{{trans|Wren}}
<
import (
Line 419:
fmt.Printf("%4.2f ..< %4.2f %s %s\n", binSize*fi, binSize*(fi+1), hist, commatize(bins[i]))
}
}</
{{out}}
Line 453:
The modifier is a pure function which consumes a sequence of numbers (probably, random) and applies a modification rule to it.
<
import Data.List
import Text.Printf
Line 473:
showHist h = foldMap mkLine h
where
mkLine (b,n) = printf "%.2f\t%s %d%%\n" b (replicate n '▇') n</
<pre>λ> showHist $ hist 0.05 $ take 50000 $ modify vShape $ randoms $ mkStdGen 1234
Line 501:
Implementation:
<
while.do.r=.?0 0
if. (<u)/r do. {:r return.end.
Line 507:
}}
mod=: {{|1-2*y}}</
Task example:
<
bins=: 17%~i.18 NB. upper bounds (0 does not appear in result)
Line 531:
0.882353 854 ###################################################
0.941176 996 ###########################################################
1 1130 ###################################################################</
=={{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)
</
<pre>
Line 571:
=={{header|Mathematica}}/{{header|Wolfram Language}}==
<
Modifier[x_] := If[x < 0.5, 2 (0.5 - x), 2 (x - 0.5)]
CreateRandomNumber[] := Module[{r1, r2, done = True},
Line 585:
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]</
{{out}}
<pre>{0.,0.05} - XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
Line 610:
=={{header|Nim}}==
{{trans|Wren}}
<
type ValRange = range[0.0..1.0]
Line 643:
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]}"</
{{out}}
Line 672:
{{trans|Raku}}
Uses any supplied distribution function, but defaults to uniform otherwise.
<
use warnings;
use List::Util 'max';
Line 706:
my @samples = distribution( function => \&modifier_notch, sample_size => 50_000);
print_histogram( data => \@samples, n_bins => 20, width => 64);</
{{out}}
<pre>Bin Counts Histogram
Line 743:
=={{header|Phix}}==
<
while true do
atom r1 := rnd()
Line 774:
sequence hist := repeat('#', round(bins[i]/HIST_CHAR_SIZE))
printf(1,"%s %s %,d\n", {LBLS[i], hist, bins[i]})
end for</
{{out}}
<pre>
Line 803:
{{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.
<
IupOpen()
Ihandle plot = IupPlot("GRID=YES, AXS_YAUTOMIN=NO")
Line 815:
IupShow(IupDialog(plot, `TITLE=Histogram, RASTERSIZE=1300x850`))
IupMainLoop()
IupClose()</
=={{header|Python}}==
<
from typing import List, Callable, Optional
Line 880:
print(f" {b / bins:5.2f}, {count:4}, {delta:>4}: "
f"{'#' * int(40 * count / mx)}")
last = count</
{{out}}
Line 902:
=={{header|R}}==
Although it may not be immediately obvious, both modifier and gen are equivalent to the corresponding functions in the task.
<
modifier <- function(x) 2*abs(x - 0.5)
gen <- function()
Line 913:
}
data <- replicate(100000, gen())
NostalgiR::nos.hist(data, breaks = 20, pch = "#")</
{{out}}
<pre>> NostalgiR::nos.hist(data, breaks = 20, pch = "#")
Line 951:
=={{header|REXX}}==
If a vertical histograph (instead of a <big><big><big><</big></big></big> shaped horizontal histograph) were to be used, it would be a <big><big>V</big></big> shaped.
<
parse arg randn bins seed . /*obtain optional argument from the CL.*/
if randN=='' | randN=="," then randN= 100000 /*Not specified? Then use the default.*/
Line 983:
#= # + 1; @.#= r /*bump counter; assign the MRD to array*/
end /*until*/
return</
{{out|output|text= when using the default inputs:}}
<pre>
Line 1,011:
=={{header|Raku}}==
<syntaxhighlight lang="raku"
return lazy gather loop {
my ( $r1, $r2 ) = rand, rand;
Line 1,031:
my @d = modified_random_distribution( &modifier );
print_histogram( @d.head(50_000), :n-bins(20), :width(64) );</
{{out}}
<pre>
Line 1,059:
=={{header|Wren}}==
{{libheader|Wren-fmt}}
<
import "/fmt" for Fmt
Line 1,093:
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])
}</
{{out}}
|