Modified random distribution: Difference between revisions

(→‎{{header|Factor}}: Add works with template. Apparently fried quotations have been moved to syntax!)
Line 215:
 
</pre>
 
=={{header|Phix}}==
<lang Phix>function rng(integer modifier)
while true do
atom r1 := rnd()
if rnd() < modifier(r1) then
return r1
end if
end while
end function
function modifier(atom x)
return iff(x < 0.5 ? 2 * (0.5 - x)
: 2 * (x - 0.5))
end function
 
constant N = 100000,
NUM_BINS = 20,
HIST_CHAR_SIZE = 125,
BIN_SIZE = 1/NUM_BINS,
LO = sq_mul(tagset(NUM_BINS-1,0),BIN_SIZE),
HI = sq_mul(tagset(NUM_BINS),BIN_SIZE),
LBLS = apply(true,sprintf,{{"[%4.2f,%4.2f)"},columnize({LO,HI})})
 
sequence bins := repeat(0, NUM_BINS)
for i=1 to N do
bins[floor(rng(modifier) / BIN_SIZE)+1] += 1
end for
printf(1,"Modified random distribution with %,d samples in range [0, 1):\n\n",N)
for i=1 to NUM_BINS do
sequence hist := repeat('#', round(bins[i]/HIST_CHAR_SIZE))
printf(1,"%s %s %,d\n", {LBLS[i], hist, bins[i]})
end for</lang>
{{out}}
<pre>
Modified random distribution with 100,000 samples in range [0, 1):
 
[0.00,0.05) ############################################################################ 9,521
[0.05,0.10) #################################################################### 8,449
[0.10,0.15) ############################################################ 7,519
[0.15,0.20) ##################################################### 6,651
[0.20,0.25) ############################################ 5,470
[0.25,0.30) #################################### 4,504
[0.30,0.35) ########################### 3,364
[0.35,0.40) #################### 2,475
[0.40,0.45) ############ 1,494
[0.45,0.50) #### 518
[0.50,0.55) #### 482
[0.55,0.60) ############ 1,536
[0.60,0.65) ##################### 2,568
[0.65,0.70) ############################ 3,498
[0.70,0.75) #################################### 4,559
[0.75,0.80) ############################################ 5,447
[0.80,0.85) #################################################### 6,512
[0.85,0.90) ############################################################ 7,486
[0.90,0.95) #################################################################### 8,484
[0.95,1.00) ############################################################################ 9,463
</pre>
A simple graphical plot. Note the labels are on the X-axis, so it's V-shaped: IupPlot does not support putting user-supplied labels on the Y-axis.
<lang Phix>include pGUI.e
IupOpen()
Ihandle plot = IupPlot("GRID=YES, AXS_YAUTOMIN=NO")
IupPlotBegin(plot, 01)
for i=1 to length(bins) do
IupPlotAddStr(plot, LBLS[i], bins[i]);
end for
{} = IupPlotEnd(plot)
IupSetAttribute(plot,"DS_MODE","BAR")
IupSetAttribute(plot,"DS_COLOR",IUP_DARK_BLUE)
IupShow(IupDialog(plot, `TITLE=Histogram, RASTERSIZE=1300x850`))
IupMainLoop()
IupClose()</lang>
 
=={{header|Python}}==
7,813

edits