Hey! I wrote a code to test uniformity of random numbers in 1D and now i need to do the same for 2D and so on until 4, but i’m new to this language and i’m struggling a bit. The code for 1D that i wrote is:
program teste1d
implicit none
real(8), dimension(:), allocatable :: v, n
real, dimension(:), allocatable :: cell
real :: c
integer :: i, k, j
logical :: cond = .true.
k = 1000000 !total of random numbers
j = 20 !number of cells
allocate(v(k)) !vector with the random numbers
allocate(cell(j)) !how many numbers fall in each cell
allocate(n(j))
call random_number(v)
!inicializing the vectors as zeros
cell = cell*0.0
n = n*0.0
!vector just to plot the result
do i=1,size(n)
n(i) = n(i) + i
end do
do i=1, size(v)
c = (1.0/j)
cond = .true.
do while(cond)
if(v(i) < c) then
cell(int(j*c)) = cell(int(j*c)) + 1 !cell 1 recieve 1
cond = .false.
else
c = c + (1.0/j)
end if
end do
end do
!just to plot the result
open(40, file = 'dados.dat', status='unknown')
do i=1,size(n)
write(40,*)n(i), cell(i)
end do
close(40)
call system ('gnuplot -p 1d.plt')
end program teste1d
Now, i was trying to do pretty much the same but i’m having difficult to acess the pair of random numbers, my code is avaluating every value instead of the one single pair of two cooordinates. The code is: (In this one i used another RNG):
program teste2d
use rf_mod, only: rf
implicit none
real(8), dimension(:,:), allocatable :: v
real, dimension(:,:), allocatable :: cell
real :: c
integer :: N=5, i, j , k
logical :: cond = .true.
k = 10
allocate(v(k,2))
allocate(cell(N,N))
cell = cell*0.0
do i=1,k
do j=1, 2
v(i,j) = rf()
end do
end do
!print *, v
do i=1, k
do j=1, 2
c = 1.0/(N)
cond = .true.
do while(cond)
if(v(i,j) < c) then
cell(int(N*c),int(N*c)) = cell(int(N*c),int(N*c)) + 1 !a celula 1 recebe 1
cond = .false.
else
c = c + (1.0/N)
end if
end do
end do
end do
print*, cell, sum(cell)
end program teste2d
module rf_mod
contains
DOUBLE PRECISION FUNCTION RF()
IMPLICIT DOUBLE PRECISION (A-H, O-Z)
data IX1,IX2,IX3,IX4,IX5,IX6/1500419,1400159,1364,1528,1,3/
RR1=1.0/FLOAT(IX1)
RR2=1.0/FLOAT(IX2)
IX5=MOD(IX5*IX3,IX1)
IX6=MOD(IX6*IX4,IX2)
RF=RR1*IX5+RR2*IX6
IF(RF.GE.1.0)RF=RF-1.0
END FUNCTION
end module
The ideia is illustrated in this figure :