subroutine decide_num_to_spawn(parent_pop, av_spawns_per_walker, nspawn)
real(dp), intent(in) :: parent_pop
real(dp), intent(in) :: av_spawns_per_walker
integer, intent(out) :: nspawn
real(dp) :: prob_extra_walker, r
nspawn = abs(int(parent_pop * av_spawns_per_walker))
if (abs(abs(parent_pop * av_spawns_per_walker) - real(nspawn, dp)) > 1.e-12_dp) then
prob_extra_walker = abs(parent_pop * av_spawns_per_walker) - real(nspawn, dp)
r = genrand_real2_dSFMT()
if (prob_extra_walker > r) nspawn = nspawn + 1
end if
end subroutine decide_num_to_spawn