c-----------------------------------------------------------
c Chapter 10: Random partition of an integer n(p75)
c-----------------------------------------------------------
c   Name of subroutine: RANPAR
c
c   Algorithm:Generating a random partition of n
c
c   input:    n=6
c   number of calls: run=50
c   complier: f77 ranpar.f
c-----------------------------------------------------------

      parameter(n=6,run=50)
      integer k,p(n),mult(n)
      do 20 m=1,run
      call ranpar(n,k,mult,p)
      write(*,30),(mult(i),i=1,n)
30    format(10(1x,i2))
20    continue
      stop
      end

c-----Subroutine begins here--------------------------------
      subroutine ranpar(n,k,mult,p)
      integer p(n),d,mult(n)
      data nlast/0/
10    if(n.le.nlast) go to 30
20    p(1)=1
      m=nlast+1
      nlast=n
      if(n.eq.1) go to 30
      do 21  i=m,n
      isum=0
26    do 22  d=1,i
      is=0
      i1=i
24    i1=i1-d
      if(i1) 22,25,23
23    is=is+p(i1)
      go to 24
25    is=is+1
22    isum=isum+is*d
21    p(i)=isum/i
30    m=n
      k=0
      do 31  i=1,n
31    mult(i)=0
40    z=rand(0)*m*p(m)
      d=0
110   d=d+1
60    i1=m
      j=0
150   j=j+1
70    i1=i1-d
80    if(i1)  110,90,120
120   z=z-d*p(i1)
130   if(z) 145,145,150
90    z=z-d
100   if(z) 145,145,110
145   mult(d)=mult(d)+j
      k=k+j
160   m=i1
170   if(m.ne.0) go to 40
      return
      end


