public inbox for fortran@gcc.gnu.org
 help / color / mirror / Atom feed
From: ch l <lchopn@gmail.com>
To: fortran@gcc.gnu.org
Subject: About the pseudo random generator in gfortran
Date: Fri, 20 May 2016 15:56:00 -0000	[thread overview]
Message-ID: <CAFJrvx35kOKTdDHdbnqxWCy6hmYbvw9dkVUUbq-=ToiW3CT0WQ@mail.gmail.com> (raw)

Hi,

I'm a ordinary user of The GNU Fortran Compiler. I'm still trying my best
to learn the Fortran programming language. When I am using The GNU Fortran
Compiler, I try to use the internal pseudo random generator like this:

call random_seed()
call random_number(rnd)

This code is quite common on many Fortran textbook or on many websites. The
2nd line might also be use in a DO loop. Although I compile and run it for
many times, I get totally SAME random number(s). I have tested the same
code on Intel 's Fortran Compiler, Intel 's Fortran Compiler will give
different number for the every time I run it.

I have tried this both on Windows (MinGW, TDM-GCC) and directly on Linux's
original GCC. They seem to have same problem. I also tried to search on
Google, but there seems no useful answer.

I have read the official wiki, the example in the function RANDOM_NUMBER
(which URL is https://gcc.gnu.org/onlinedocs/gfortran/RANDOM_005fNUMBER.html
) use a new SUBRUTINE named init_random_seed, and its define is in the
RANDOM_SEED page (
https://gcc.gnu.org/onlinedocs/gfortran/RANDOM_005fSEED.html#RANDOM_005fSEED).
So I have tried the init_random_seed subroutine in my own code, it seems
works well, I get the different number for every times I run the program.

But I'm really confused that why GNU's Fortran Compiler requires a external
subroutine to generate random number. So I wonder if this is a bug or it
is just designed to behave like this. It will be better if you can teach me
a lot. Maybe there is a more elegant way to generate random number. ;)

I'm very sorry to disturb you, but I hope you can reply to me soon. Thanks
a lot.

Best regards,
lch

             reply	other threads:[~2016-05-20 15:56 UTC|newest]

Thread overview: 2+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2016-05-20 15:56 ch l [this message]
2016-05-20 20:12 ` Jerry DeLisle

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to='CAFJrvx35kOKTdDHdbnqxWCy6hmYbvw9dkVUUbq-=ToiW3CT0WQ@mail.gmail.com' \
    --to=lchopn@gmail.com \
    --cc=fortran@gcc.gnu.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).