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 in interval of [-1,+1] 1

Status
Not open for further replies.

Am_Ine

Programmer
Jan 13, 2020
4
DZ
Hi everyone,
I'm a new user of FORTRAN and i would like to write a subroutine in fortran to generate a random number between [-1 , +1]
actually i have written a simple one but , the number generated is between [0, +1] and in each time of compilation i have the same random number generated

i need help to fix this problem
i'm waiting for your answers ,
thanks in advance
 
Hi Am_Ine,

You can generate random number 0 <= r <= 1 using the subroutine random_number(r)
Here is an example:
Then if you have r from [0, 1] then you can create a random number q from [A, B] as
q = A + (B - A) * r
so in your case for q from [-1 1] it will be
q = -1 + 2 * r
 
thank you mikrom for your rapid and helpful answer
sorry but , please i have just one more question,
to generate differente random numbers each compilation , the function'' init_random-seed()'' doesn't works with me
i get :

/tmp/cc4kPuvl.o: In function `MAIN__':
main.f95:(.text+0x18): undefined reference to `init_random_seed_'
collect2: error: ld returned 1 exit status

do you have anby solution for that
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top