![forums.silverfrost.com Forum Index](templates/subSilver/images/logo_phpBB.gif) |
forums.silverfrost.com Welcome to the Silverfrost forums
|
View previous topic :: View next topic |
Author |
Message |
umike
Joined: 19 Sep 2011 Posts: 4
|
Posted: Mon Sep 19, 2011 1:07 pm Post subject: random_number |
|
|
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? |
|
Back to top |
|
![](templates/subSilver/images/spacer.gif) |
JohnCampbell
Joined: 16 Feb 2006 Posts: 2560 Location: Sydney
|
Posted: Mon Sep 19, 2011 2:20 pm Post subject: |
|
|
Does this example reproduce what you are finding ? Code: | integer*4, parameter :: million = 1000000
real*8 x, sx,sxx, n
integer*4 i
!
sx = 0 ; sxx = 0 ! opps, programming error !!!
do i = 1, million
call random_number (x)
sx = sx + x
sxx = sxx + x*x
end do
!
n = i-1.0d0
sx = sx / n
sxx = sqrt ( (sxx - n*sx*sx) / (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
|
Last edited by JohnCampbell on Tue Sep 20, 2011 12:50 am; edited 1 time in total |
|
Back to top |
|
![](templates/subSilver/images/spacer.gif) |
davidb
Joined: 17 Jul 2009 Posts: 560 Location: UK
|
Posted: Mon Sep 19, 2011 8:42 pm Post subject: |
|
|
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?
Code: |
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 _________________ Programmer in: Fortran 77/95/2003/2008, C, C++ (& OpenMP), java, Python, Perl |
|
Back to top |
|
![](templates/subSilver/images/spacer.gif) |
umike
Joined: 19 Sep 2011 Posts: 4
|
Posted: Fri Oct 07, 2011 3:28 pm Post subject: |
|
|
yes it is solved,thank you for attention but it was my mistake |
|
Back to top |
|
![](templates/subSilver/images/spacer.gif) |
|
|
You cannot post new topics in this forum You cannot reply to topics in this forum You cannot edit your posts in this forum You cannot delete your posts in this forum You cannot vote in polls in this forum
|
Powered by phpBB © 2001, 2005 phpBB Group
|