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

Students Click Here

Random Number Generator Between 0 and 1?

Random Number Generator Between 0 and 1?

Random Number Generator Between 0 and 1?

(OP)
Hi Guys,
Just needed a bit of help writing a program that gives a number between 0 and 1.

I have so far written:

===============================
Program Main
Implicit None
    Integer :: i, count, j=1

    write(*,*) "how many times do you want to generate?"
    read(*,*) count

    call random_seed(1)
        Do i=1,count
                call random_number(j)
        Write(*,*) "the random number is" j
    End Do
    
End Program Main

===============================

So far the program keeps giving me huge numbers (positive and negative)

I only want numbers between 0 and 1. Any ideas please?

Many thanks
==========================

RE: Random Number Generator Between 0 and 1?


How can you expect, that j will be between 0 and 1 when you declared it as an integer?
If you try to compile your program with gfortran you will get this error:

CODE

$ gfortran rnd.f95 -o rnd
rnd.f95:8.19:

  call random_seed(1)
                  1
Error: 'size' argument of 'random_seed' intrinsic at (1) must be a variable
rnd.f95:10.23:

    call random_number(j)
                      1
Error: 'harvest' argument of 'random_number' intrinsic at (1) must be REAL
The compiler says all. Change the program to this

CODE

program main
  implicit none
  integer :: i, count, seed
  real :: r

  write(*,*) "how many times do you want to generate?"
  read(*,*) count

  call random_seed(seed)
  do i=1,count
    call random_number(r)
    Write(*,*) "the random number is", r
  end Do
    
end Program main
and it will work as you expected

CODE

$ gfortran rnd.f95 -o rnd

$ rnd
 how many times do you want to generate?
5
 the random number is  0.99755955    
 the random number is  0.56682467    
 the random number is  0.96591532    
 the random number is  0.74792767    
 the random number is  0.36739087    

RE: Random Number Generator Between 0 and 1?

(OP)
Thanks for that. ofcourse, i overlooked the real issue :p

Just a note though, everytime I run the program, it keeps generating the same sequence of random numbers.
So in fact, I'm not acutally getting a random sequence each time I run the program.

Any ways to solve this?


By the way, are there any good compilers available?
I'm using PLATO by silverfrost, but when i compile the program and run it, all integers seem to have a BIG padding before them (a huge space for no reason)?



Thanks

RE: Random Number Generator Between 0 and 1?

Instead of

CODE

call random_seed(seed)
try to use

CODE

call init_random_seed()
The subroutine is defined here:
http://gcc.gnu.org/onlinedocs/gfortran/RANDOM_005fSEED.html#RANDOM_005fSEED

I tried it in gfortran and it works - it generates everytime different random numbers:

CODE

$ gfortran rnd.f95 -o rnd

$ rnd
 how many times do you want to generate?
3
 the random number is  4.18258309E-02
 the random number is  0.95481700    
 the random number is  0.38921034    

$ rnd
 how many times do you want to generate?
3
 the random number is  0.95889604    
 the random number is  0.78550929    
 the random number is  0.70096797
    
$ rnd
 how many times do you want to generate?
3
 the random number is  2.19990611E-02
 the random number is  0.50352961    
 the random number is  0.90276980    
But with g95 it doesn't work - it generates everytime same numbers:

CODE

$ g95 rnd.f95 -o rnd

$ rnd
 how many times do you want to generate?
3
 the random number is 0.8157187
 the random number is 0.9665488
 the random number is 0.8606074

$ rnd
 how many times do you want to generate?
3
 the random number is 0.8157187
 the random number is 0.9665488
 the random number is 0.8606074
I don't know why...

Quote (androidmelt):


By the way, are there any good compilers available?
As I mentioned I'm using g95, gfortran and g77. All are free available.
 

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