random numbers in C++/Fortran 90 by 바죠

 a.out
1122
2147483647
0.500041 0.0833564
0.68



#include <iostream>
#include<ctime>
#include<math.h>
using namespace std;

int main()
{
        int i, iseed;
        int MAX;
        double a ;
        double b ;
        double x ;

        MAX=10000000;
        a=0.e0 ;
        b=0.e0 ;

        std::clock_t start=std::clock();
        std::clock_t end=std::clock();


        iseed=time(NULL);
        iseed=1122 ;
        cout << iseed << endl;
        cout << RAND_MAX << endl;
        srand(iseed);

        a=0.e0 ;
        b=0.e0 ;
                start=std::clock();
        for (i =0 ; i < MAX ; i++)
        {
                x=(double )  rand() /(double ) RAND_MAX ;
                a=a+x ;
                b=b+(x-0.5)*(x-0.5) ;
        }

        a=a/(double ) MAX ;
        b=b/(double ) MAX ;
                end=std::clock();
        cout << a<< ' '<< b << endl;

        cout << (end-start)/(double) CLOCKS_PER_SEC << endl ;


                return 0;
}


그림 출처: google 이미지



 

-------------------------


random number module from:
http://www.sdsc.edu/~tkaiser/f90.html

!234567890
module ran_mod
! ran1 returns a uniform random number between 0-1
! the seed is optional and used to reset the generator
contains
   function ran1(my_seed)
      implicit none
      real(8) ran1,r
      integer, optional ,intent(in) :: my_seed  ! optional argument not changed in the routine
      integer,allocatable :: seed(:)
      integer the_size,j
      if(present(my_seed))then            ! use the seed if present
          call random_seed(size=the_size) ! how big is the intrisic seed?
          allocate(seed(the_size))        ! allocate space for seed
          do j=1,the_size                 ! create the seed
             seed(j)=abs(my_seed)+(j-1)   ! abs is generic
          enddo
          call random_seed(put=seed)      ! assign the seed
          deallocate(seed)                ! deallocate space
      endif
      call random_number(r)
      ran1=r
  end function ran1
end module


program darwin
    use ran_mod          ! interface required if we have
                         ! optional or intent arguments
    real(8) x,y
    x=ran1(my_seed=12345) ! we can specify the name of the argument
    y=ran1()
    write(*,*)x,y
    x=ran1(12345)         ! with only one optional argument we don't need to
    y=ran1()
    write(*,*)x,y
end program


 a.out
   2.9463771517441728E-003   2.9468539889592194E-003
   2.9463771517441728E-003   2.9468539889592194E-003


-------------------------------------

random number in FORTRAN 90

!234567890
implicit none
integer nseed
real*8 tmp
integer i

nseed=111
call random_seed(nseed)
do i=1,10
call random_number(tmp)
print*, tmp
enddo

print*, ''
nseed=222
call random_seed(nseed)
do i=1,10
call random_number(tmp)
print*, tmp
enddo

stop
end


a.out
0.9079229567097968
0.1906920678350303
6.7165295567662042E-002
0.8000845082947166
0.7973146090875929
0.6368299929250867
0.5887827502396021
0.1590656082684347
0.3206476984578757
0.4481760997188218

0.6579501647005799
0.6075459015925730
0.7622636965428597
0.4374797344870984
0.4638690559296634
0.7004157172580108
0.6280164305854896
0.6701866532526566
0.6281718023221288
0.5310343514973255
FORTRAN STOP


http://incredible.egloos.com/4832215

http://incredible.egloos.com/7378157



cat seed.f90
   program main
   implicit none
!  integer, parameter :: wp = selected_real_kind(15,307)
   integer, parameter :: ndiscard = 100

   integer :: nstate_size, i
   integer, allocatable, dimension(:) :: istate
!  real(wp) :: ran, oldran
   real*8 :: ran, oldran

   call random_seed(size=nstate_size)
   allocate(istate(nstate_size))
   istate = 20180815
   istate = 20180814
   call random_seed(put=istate)
   deallocate(istate)
!  ran = 0.5_wp
   ran = 0.5d0
   do i=1,ndiscard
      oldran = ran
      call random_number(ran)
   enddo
   end program main




핑백

덧글

댓글 입력 영역

최근 포토로그



MathJax