How to resolve the algorithm Verify distribution uniformity/Naive step by step in the Fortran programming language

Published on 12 May 2024 09:40 PM

How to resolve the algorithm Verify distribution uniformity/Naive step by step in the Fortran programming language

Table of Contents

Problem Statement

This task is an adjunct to Seven-sided dice from five-sided dice.

Create a function to check that the random integers returned from a small-integer generator function have uniform distribution.

The function should take as arguments:

The function should produce:

Show the distribution checker working when the produced distribution is flat enough and when it is not. (Use a generator from Seven-sided dice from five-sided dice).

See also:

Let's start with the solution:

Step by Step solution about How to resolve the algorithm Verify distribution uniformity/Naive step by step in the Fortran programming language

Source code in the fortran programming language

subroutine distcheck(randgen, n, delta)

  interface
    function randgen
      integer :: randgen 
    end function randgen
  end interface
  
  real, intent(in) :: delta
  integer, intent(in) :: n
  integer :: i, mval, lolim, hilim
  integer, allocatable :: buckets(:)
  integer, allocatable :: rnums(:)
  logical :: skewed = .false.
     
  allocate(rnums(n))
  
  do i = 1, n
    rnums(i) = randgen()
  end do

  mval = maxval(rnums)
  allocate(buckets(mval))
  buckets = 0
  
  do i = 1, n
    buckets(rnums(i)) = buckets(rnums(i)) + 1
  end do

  lolim = n/mval - n/mval*delta
  hilim = n/mval + n/mval*delta
  
  do i = 1, mval  
    if(buckets(i) < lolim .or. buckets(i) > hilim) then
      write(*,"(a,i0,a,i0,a,i0)") "Distribution potentially skewed for bucket ", i, "   Expected: ", &
                                   n/mval, "   Actual: ", buckets(i)
      skewed = .true.
    end if
  end do 

  if (.not. skewed) write(*,"(a)") "Distribution uniform"
  
  deallocate(rnums)
  deallocate(buckets)
    
end subroutine


  

You may also check:How to resolve the algorithm Associative array/Creation step by step in the Potion programming language
You may also check:How to resolve the algorithm XML/Input step by step in the jq programming language
You may also check:How to resolve the algorithm Regular expressions step by step in the GeneXus programming language
You may also check:How to resolve the algorithm Evolutionary algorithm step by step in the ALGOL 68 programming language
You may also check:How to resolve the algorithm Order two numerical lists step by step in the AWK programming language