Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Random Number

Status
Not open for further replies.

niloy112112

Programmer
Feb 14, 2014
5
CA
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
 
Please at least first do a minimal google search, something like "fortran90 random number" or something...then attempt to put a minimal program with that and then we shall see what kind of help you need.
 
Thank you for quick reply.
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
 
Given the situation, I will dare to say that you forget everything and start from scratch...it is not difficult to learn how to generate random number...read this and and this other pages and put together a minimal program that uses these functions.

 
Hello,
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
 
Whatever...forget about the second link...is too much at this time.

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.
 
Thank you for the mail. It helped me a lot. I appreciate it.
 
Note:

as FJacq has alerted us here
FJacq said:
... instead of rand(), you should use the standard subroutine random_number ...
and according to this description of RAND
which says, that
This intrinsic routine is provided for backwards compatibility with GNU Fortran 77. It implements a simple modulo generator as provided by g77. For new code, one should consider the use of RANDOM_NUMBER as it implements a superior algorithm.
you should instead of rand preferably use the subroutine random_number - see:
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top