×
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!
  • Students Click Here

*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.

Students Click Here

random number in interval of [-1,+1]

random number in interval of [-1,+1]

random number in interval of [-1,+1]

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

RE: random number in interval of [-1,+1]

Hi Am_Ine,

You can generate random number 0 <= r <= 1 using the subroutine random_number(r)
Here is an example: https://www.tek-tips.com/viewthread.cfm?qid=159415...

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

RE: random number in interval of [-1,+1]

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

RE: random number in interval of [-1,+1]

(OP)
thank you mikrom for your help smile

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! Already a Member? Login

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