Fortran How can I generate a random number between 1-10 in Fortran 90?

AI Thread Summary
To generate a random number between 1 and 10 in a Fortran program, the current implementation needs adjustments. The random number generator typically produces values between 0 and 1, so scaling is necessary. To achieve a number in the desired range, multiply the generated value by 10 and then add 1. For integer results, apply a rounding function. Additionally, to avoid receiving the same sequence of numbers on each run, initialize the random number generator with a seed using the CALL SEED(ii) function, where 'ii' is an integer. This ensures that different seeds will yield different sequences of random numbers.
epratt
Messages
1
Reaction score
0
I'm trying to contruct a program that will generate a different random number between 1-10. I am not sure how to make it only 1-10.
PROGRAM guess
USE const
IMPLICIT NONE

INTEGER::i
REAL(kind=dp)::x

call random_number(x)
WRITE(*,*) 'x=',x
END DO

END PROGRAM guess

This gives me the same value every time of
Z:\CPS201>guess.exe
x= 3.920868194323862E-007
 
Technology news on Phys.org
Typically random number generators will give you a random between 0 and 1, so if you want a number between 1 and 10, you'll have to add/scale appropriately. If you need an integer, you'll need to use some rounding function.
 
Also, the program will give the same sequence of numbers every time, since one has to initialize the 'seed', which can be done according to CALL SEED(ii), where i is an integer. Different integers will give different sequences of random numbers.
 
Dear Peeps I have posted a few questions about programing on this sectio of the PF forum. I want to ask you veterans how you folks learn program in assembly and about computer architecture for the x86 family. In addition to finish learning C, I am also reading the book From bits to Gates to C and Beyond. In the book, it uses the mini LC3 assembly language. I also have books on assembly programming and computer architecture. The few famous ones i have are Computer Organization and...
I have a quick questions. I am going through a book on C programming on my own. Afterwards, I plan to go through something call data structures and algorithms on my own also in C. I also need to learn C++, Matlab and for personal interest Haskell. For the two topic of data structures and algorithms, I understand there are standard ones across all programming languages. After learning it through C, what would be the biggest issue when trying to implement the same data...

Similar threads

Replies
4
Views
2K
Replies
5
Views
5K
Replies
12
Views
3K
Replies
2
Views
1K
Replies
4
Views
2K
Back
Top