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 Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Random Number Generator Between 0 and 1?

Status
Not open for further replies.

androidmelt

Programmer
Mar 3, 2010
6
GB
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
==========================
 

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:
[COLOR=#a020f0]program[/color] main
  [COLOR=#2e8b57][b]implicit[/b][/color] [COLOR=#2e8b57][b]none[/b][/color]
  [COLOR=#2e8b57][b]integer[/b][/color] :: i, [COLOR=#008080]count[/color], seed
[COLOR=#2e8b57][b]  real[/b][/color] :: r

  [COLOR=#804040][b]write[/b][/color]([COLOR=#804040][b]*[/b][/color],[COLOR=#804040][b]*[/b][/color]) [COLOR=#ff00ff]"how many times do you want to generate?"[/color]
  [COLOR=#804040][b]read[/b][/color]([COLOR=#804040][b]*[/b][/color],[COLOR=#804040][b]*[/b][/color]) [COLOR=#008080]count[/color]

  [COLOR=#a020f0]call[/color] [COLOR=#008080]random_seed[/color](seed) 
  [COLOR=#804040][b]do[/b][/color] i[COLOR=#804040][b]=[/b][/color][COLOR=#ff00ff]1[/color],[COLOR=#008080]count[/color]
    [COLOR=#a020f0]call[/color] [COLOR=#008080]random_number[/color](r)
    [COLOR=#804040][b]Write[/b][/color]([COLOR=#804040][b]*[/b][/color],[COLOR=#804040][b]*[/b][/color]) [COLOR=#ff00ff]"the random number is"[/color], r
  [COLOR=#804040][b]end Do[/b][/color]
    
[COLOR=#a020f0]end Program[/color] 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
 
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
 
Instead of
Code:
call random_seed(seed)
try to use
Code:
call init_random_seed()
The subroutine is defined here:

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

androidmelt said:
By the way, are there any good compilers available?
As I mentioned I'm using g95, gfortran and g77. All are free available.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top