How to resolve the algorithm Modified random distribution step by step in the REXX programming language
Published on 12 May 2024 09:40 PM
How to resolve the algorithm Modified random distribution step by step in the REXX programming language
Table of Contents
Problem Statement
Given a random number generator, (rng), generating numbers in the range 0.0 .. 1.0 called rgen, for example; and a function modifier(x) taking an number in the same range and generating the probability that the input should be generated, in the same range 0..1; then implement the following algorithm for generating random numbers to the probability given by function modifier:
Show your output here, on this page.
Let's start with the solution:
Step by Step solution about How to resolve the algorithm Modified random distribution step by step in the REXX programming language
Source code in the rexx programming language
/*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.*/
if bins=='' | bins=="," then bins= 20 /* " " " " " " */
if datatype(seed, 'W') then call random ,,seed /* " " " " " " */
call MRD
!.= 0
do j=1 for randN; bin= @.j*bins%1
!.bin= !.bin + 1 /*bump the applicable bin counter. */
end /*j*/
mx= 0
do k=1 for randN; mx= max(mx, !.k) /*find the maximum, used for histograph*/
end /*k*/
say ' bin'
say '────── ' center('(with ' commas(randN) " samples", 80 - 10)
do b=0 for bins; say format(b/bins,2,2) copies('■', 70*!.b%mx)" " commas(!.b)
end /*b*/
exit 0
/*──────────────────────────────────────────────────────────────────────────────────────*/
commas: arg ?; do jc=length(?)-3 to 1 by -3; ?=insert(',', ?, jc); end; return ?
rand: return random(0, 100000) / 100000
/*──────────────────────────────────────────────────────────────────────────────────────*/
modifier: parse arg y; if y<.5 then return 2 * (.5 - y)
else return 2 * ( y - .5)
/*──────────────────────────────────────────────────────────────────────────────────────*/
MRD: #=0; @.= /*MRD: Modified Random distribution. */
do until #==randN; r= rand() /*generate a random number; assign bkup*/
if rand()>=modifier(r) then iterate /*Doesn't meet requirement? Then skip.*/
#= # + 1; @.#= r /*bump counter; assign the MRD to array*/
end /*until*/
return
You may also check:How to resolve the algorithm Euler method step by step in the Factor programming language
You may also check:How to resolve the algorithm Primality by Wilson's theorem step by step in the Nim programming language
You may also check:How to resolve the algorithm Hello world/Standard error step by step in the R programming language
You may also check:How to resolve the algorithm 100 doors step by step in the GML programming language
You may also check:How to resolve the algorithm Stack traces step by step in the Tcl programming language