Modified random distribution: Difference between revisions

m
syntax highlighting fixup automation
(J)
m (syntax highlighting fixup automation)
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 218:
=={{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:
 
"Modified random distribution of values in [0, 1):" print nl
100,000 [ modifier ] 20 data .histogram</langsyntaxhighlight>
{{out}}
<pre>
Line 278:
 
=={{header|FreeBASIC}}==
<langsyntaxhighlight lang="freebasic">#define NRUNS 100000
#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</langsyntaxhighlight>
{{out}}
<pre>
Line 355:
=={{header|Go}}==
{{trans|Wren}}
<langsyntaxhighlight lang="go">package main
 
import (
Line 419:
fmt.Printf("%4.2f ..< %4.2f %s %s\n", binSize*fi, binSize*(fi+1), hist, commatize(bins[i]))
}
}</langsyntaxhighlight>
 
{{out}}
Line 453:
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:
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 501:
Implementation:
 
<langsyntaxhighlight Jlang="j">probmod=: {{y
while.do.r=.?0 0
if. (<u)/r do. {:r return.end.
Line 507:
}}
 
mod=: {{|1-2*y}}</langsyntaxhighlight>
 
Task example:
 
<langsyntaxhighlight Jlang="j"> rnd=: mod probmod"0 i.1e4
 
bins=: 17%~i.18 NB. upper bounds (0 does not appear in result)
Line 531:
0.882353 854 ###################################################
0.941176 996 ###########################################################
1 1130 ###################################################################</langsyntaxhighlight>
 
 
=={{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 571:
 
=={{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 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]</langsyntaxhighlight>
{{out}}
<pre>{0.,0.05} - XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
Line 610:
=={{header|Nim}}==
{{trans|Wren}}
<langsyntaxhighlight Nimlang="nim">import random, strformat, strutils, sugar
 
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]}"</langsyntaxhighlight>
 
{{out}}
Line 672:
{{trans|Raku}}
Uses any supplied distribution function, but defaults to uniform otherwise.
<langsyntaxhighlight lang="perl">use strict;
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);</langsyntaxhighlight>
{{out}}
<pre>Bin Counts Histogram
Line 743:
 
=={{header|Phix}}==
<langsyntaxhighlight Phixlang="phix">function rng(integer modifier)
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</langsyntaxhighlight>
{{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.
<langsyntaxhighlight Phixlang="phix">include pGUI.e
IupOpen()
Ihandle plot = IupPlot("GRID=YES, AXS_YAUTOMIN=NO")
Line 815:
IupShow(IupDialog(plot, `TITLE=Histogram, RASTERSIZE=1300x850`))
IupMainLoop()
IupClose()</langsyntaxhighlight>
 
=={{header|Python}}==
<langsyntaxhighlight lang="python">import random
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</langsyntaxhighlight>
 
{{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.
<langsyntaxhighlight lang="rsplus">library(NostalgiR) #For the textual histogram.
modifier <- function(x) 2*abs(x - 0.5)
gen <- function()
Line 913:
}
data <- replicate(100000, gen())
NostalgiR::nos.hist(data, breaks = 20, pch = "#")</langsyntaxhighlight>
{{out}}
<pre>> NostalgiR::nos.hist(data, breaks = 20, pch = "#")
Line 951:
=={{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 983:
#= # + 1; @.#= r /*bump counter; assign the MRD to array*/
end /*until*/
return</langsyntaxhighlight>
{{out|output|text=&nbsp; when using the default inputs:}}
<pre>
Line 1,011:
 
=={{header|Raku}}==
<syntaxhighlight lang="raku" perl6line>sub modified_random_distribution ( Code $modifier --> Seq ) {
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) );</langsyntaxhighlight>
{{out}}
<pre>
Line 1,059:
=={{header|Wren}}==
{{libheader|Wren-fmt}}
<langsyntaxhighlight lang="ecmascript">import "random" for Random
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])
}</langsyntaxhighlight>
 
{{out}}
10,333

edits