hi,i use ftn95 on windows 7,and the problem is that random_number does not give uniform distribution at all.the probabilty in the interval from 0 to 1 rises greatly any suggestions?
random_number
Does this example reproduce what you are finding ? integer4, parameter :: million = 1000000 real8 x, sx,sxx, n integer4 i ! sx = 0 ; sxx = 0 ! opps, programming error !!! do i = 1, million call random_number (x) sx = sx + x sxx = sxx + xx end do ! n = i-1.0d0 sx = sx / n sxx = sqrt ( (sxx - nsxsx) / (n-1) ) write (,1001)' Number of samples = ',n x = 0.5d0 write (,1002)' Average = ',sx, ' ( expected = ',x,' error = ',sx-x x = 1.0d0/sqrt(12.0d0) write (*,1002)' Standard deviation = ',sxx,' ( expected = ',x,' error = ',sxx-x 1001 format (a,f0.2) 1002 format (a,f0.9,a,f0.9,a,es14.6) ! end
NO ERRORS [<main program> FTN95/Win32 v4.9.0]
Creating executable: C:\\temp\\lgotemp@.exe
Program entered
Number of samples = 1000000.00
Average = 0.499785607 ( expected = 0.500000000 error = -2.143933E-04
Standard deviation = 0.288603871 ( expected = 0.288675135 error = -7.126348E-05
The following code puts the first 1000000 random values in a histogram with 20 classes each of width 0.05. Combine this with John's code above which shows the sample mean and standard deviations match approximately the theoretical values and it would seem the numbers are reasonably uniform across 0 to 1.
My code is run on Windows Vista. I don't see how it would be different on Windows 7.
Can you post some code to show what you are doing?
PROGRAM ANON
IMPLICIT NONE
INTEGER, PARAMETER :: N = 1000000
INTEGER I, BIN, HIST(20)
REAL*8 X
HIST = 0
DO I=1, N
! GET RANDOM NUMBER X SUCH THAT 0.0D0 <= X < 1.0D0
CALL RANDOM_NUMBER(X)
! Since X >=0 smallest possible BIN value is 1
! Since X < 1 largest possible BIN value is 20
BIN = INT(X / 0.05D0) + 1
HIST(BIN) = HIST(BIN) + 1
END DO
DO BIN=1, 20
PRINT *, BIN, HIST(BIN)
END DO
END PROGRAM
Resuts obtained are:
1 50091
2 49911
3 49894
4 50163
5 49874
6 50266
7 50193
8 49883
9 50026
10 49986
11 50303
12 49892
13 50042
14 49975
15 49724
16 49765
17 49933
18 50343
19 50066
20 49670
yes it is solved,thank you for attention but it was my mistake