## Random Number

## Random Number

(OP)

Hello All,

I want to generate random integer numbers in fortran.How can I do that, any idea? As an example.

Say i have, 1,2,3,4,5,6,7,8,9,10.

I want to pick this number randomly. How can i do that?

Also, if the random numbers are sspace filling then it will be great.

Thank you in advance.

Niloy

I want to generate random integer numbers in fortran.How can I do that, any idea? As an example.

Say i have, 1,2,3,4,5,6,7,8,9,10.

I want to pick this number randomly. How can i do that?

Also, if the random numbers are sspace filling then it will be great.

Thank you in advance.

Niloy

## RE: Random Number

## RE: Random Number

Well in fact i have already done some Google search. but my program understanding skill is not that good.

1)It is possible to make generate random number(RAND) but each and every time the value changes and the values are decimal numbers. I need something that i can repeat. like for 10 or 20 points the random number will always be the same and at the same time it will try to fill the whole region also.

2) Right now i am using this code but the problem is that it gives decimal values.

!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!

SUBROUTINE LHD(VRBL,N,FI_FRS)

!!!!VRBL=NUMBER OF VARIABLES..SAY 2 VARIABLE...

!!!!N= NUMBER OF RUN...SAY 10 RUN.....

!!!FOR THE ABOVE CONDITION THIS SUBROUTINE PRODUCE FOLLOWING VALUES

!!! 2.8117118E-02 2.8602943E-02

!!! 0.9628103 0.8176457

!!! 0.7940205 0.6383985

!!! 7.8667834E-02 0.9008917

!!! 0.5960708 0.6908193

!!! 0.9782130 0.9781191

!!! 5.6456625E-02 3.9342672E-02

!!! 0.3176425 0.2716489

!!! 3.4892291E-02 0.7664266

!!! 8.8317253E-02 0.6975412

IMPLICIT NONE

INTEGER::I,J,vrbl,N

REAL*4::RS1(vrbl,N),RS2(vrbl,N),SS1(vrbl,N+1),SS2(vrbl,N+1),FRS1(vrbl,N)

REAL*4::FRS2(vrbl,N),D_FRS(vrbl,N),FI_FRS(vrbl,N)

DOUBLE PRECISION::MU1(vrbl,N),MU2(vrbl,N),A1(vrbl),A2(vrbl),M1(vrbl),M2(vrbl),S1(vrbl),S2(vrbl)

OPEN( 59, FILE='59.txt')

print*,'1'

do i=1,vrbl

A1(i)=45614+699*i;

A2(i)=50692+599*i;

M1(i)=2147483563+5000*I;

M2(i)=2147483399+2500*I;

S1(i)=1362+13*i;

S2(i)=3567+29*i;

SS1(i,1)=S1(i)

SS2(i,1)=S2(i)

END DO

DO j=1,vrbl

DO I=1,N

MU1(j,I)=A1(j)*SS1(j,I)

MU2(j,I)=A2(j)*SS2(j,I)

RS1(j,I)=MOD(MU1(j,I),M1(j))

RS2(j,I)=MOD(MU2(j,I),M2(j))

FRS1(j,I)=RS1(j,I)/M1(j)

FRS2(j,I)=RS2(j,I)/M2(j)

D_FRS(j,I)=(FRS1(j,I)-FRS2(j,I))

IF (D_FRS(j,I).GT.0.0d0) THEN

FI_FRS(j,I)=(D_FRS(j,I)/2)

ELSE IF (D_FRS(j,I).LT.0.0d0)THEN

FI_FRS(j,I)=((D_FRS(j,I)/2)+1)

ELSE IF (D_FRS(j,I).EQ.0.0d0)THEN

FI_FRS(j,I)=1.0

END IF

SS1(j,I+1)=RS1(j,I)

SS2(j,I+1)=RS2(j,I)

END DO

END DO

print*,'2'

DO I=1,N

WRITE(59,*)FI_FRS(1,I),FI_FRS(2,I)

END DO

write(*,*) FI_FRS

END SUBROUTINE

!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!

Thanks,

Niloy

## RE: Random Number

## RE: Random Number

Thank you for the quick reply.

Yes i forgot everything. In fact i did not know it.

In your first link..it gives floating number as result.(which is not required by me)

In the second link there is a term int64..this does not works on my fortran(version 20070215)or may be i dont know how to use it.

You told to put together..well i was failed to do so.

Niloy

## RE: Random Number

To generate random number between 0.0 and 1.0, you start with the rand() function.

To generate random integers between 0 and N, you simply take N times the result of rand() and then truncate:

randomInteger = int( N*rand() )

To generate a random integer within a given range (N1, N2) you do:

randomInteger = N1 + int( (N2 - N1)*rand() )

Please study and understand this and test it with a minimal program.

## RE: Random Number

## RE: Random Number

Note:as

FJacqhas alerted us herehttp://www.tek-tips.com/viewthread.cfm?qid=1730131

and according to this description of

RANDhttps://gcc.gnu.org/onlinedocs/gfortran/RAND.html

which says, that

you should instead of

randpreferably use the subroutinerandom_number- see:https://gcc.gnu.org/onlinedocs/gfortran/RANDOM_005...