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:

while True:
    random1 = rgen()
    random2 = rgen()
    if random2 < modifier(random1):
        answer = random1
        break
    endif
endwhile
Task

 Create a modifier function that generates a 'V' shaped probability of number generation using something like, for example:
                      modifier(x)  =  2*(0.5 - x)  if x < 0.5  else 2*(x - 0.5) 

 Create a generator of random numbers with probabilities modified by the above function.
 Generate >= 10,000 random numbers subject to the probability modification.
 Output a textual histogram with from 11 to 21 bins showing the distribution of the random numbers generated.


Show your output here, on this page.




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