INTELLIGENT WORK FORUMS
FOR COMPUTER PROFESSIONALS

Log In

Come Join Us!

Are you a
Computer / IT professional?
Join Tek-Tips Forums!
  • Talk With Other Members
  • Be Notified Of Responses
    To Your Posts
  • Keyword Search
  • One-Click Access To Your
    Favorite Forums
  • Automated Signatures
    On Your Posts
  • Best Of All, It's Free!

*Tek-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

Posting Guidelines

Promoting, selling, recruiting, coursework and thesis posting is forbidden.

Jobs

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

RE: Random Number

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.

RE: Random Number

(OP)
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

RE: Random Number

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.

RE: Random Number

(OP)
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

RE: Random Number

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.

RE: Random Number

(OP)
Thank you for the mail. It helped me a lot. I appreciate it.

RE: Random Number

Note:

as FJacq has alerted us here
http://www.tek-tips.com/viewthread.cfm?qid=1730131

Quote (FJacq)


... instead of rand(), you should use the standard subroutine random_number ...
and according to this description of RAND
https://gcc.gnu.org/onlinedocs/gfortran/RAND.html
which says, that

Quote:


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:
https://gcc.gnu.org/onlinedocs/gfortran/RANDOM_005...

Red Flag This Post

Please let us know here why this post is inappropriate. Reasons such as off-topic, duplicates, flames, illegal, vulgar, or students posting their homework.

Red Flag Submitted

Thank you for helping keep Tek-Tips Forums free from inappropriate posts.
The Tek-Tips staff will check this out and take appropriate action.

Reply To This Thread

Posting in the Tek-Tips forums is a member-only feature.

Click Here to join Tek-Tips and talk with other members!

Resources

Close Box

Join Tek-Tips® Today!

Join your peers on the Internet's largest technical computer professional community.
It's easy to join and it's free.

Here's Why Members Love Tek-Tips Forums:

Register now while it's still free!

Already a member? Close this window and log in.

Join Us             Close