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

In summary, the program discussed is attempting to generate a random number between 1-10, but is currently only producing the same value every time it is run. To resolve this issue, the program needs to scale the random number appropriately and initialize the seed using an integer.
  • #1
epratt
1
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
  • #2
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.
 
  • #3
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.
 

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

How do I generate a random number in Fortran 90?

In Fortran 90, you can use the RANDOM_NUMBER intrinsic function to generate a random number. It takes two arguments - a real number variable to store the random number, and an optional KIND specifier to specify the precision of the random number. For example, RANDOM_NUMBER(x) will generate a random number between 0 and 1 and store it in the variable x.

Can I specify a range for the random numbers generated in Fortran 90?

Yes, you can use the RANDOM_SEED intrinsic subroutine to specify the range for the random numbers. It takes an integer array as an argument, which serves as the seed for the random number generator. You can set the elements of the array to specify the lower and upper bounds for the random numbers. For example, RANDOM_SEED([1, 10]) will generate random numbers between 1 and 10.

How do I generate a specific type of random number in Fortran 90?

Fortran 90 allows you to generate different types of random numbers using the RANDOM_NUMBER function. You can specify the type of random number by using the KIND specifier in the function. For example, RANDOM_NUMBER(x, KIND='NORMAL') will generate a random number from a normal distribution and store it in the variable x.

Can I generate multiple random numbers at once in Fortran 90?

Yes, you can use the RANDOM_NUMBER function in a loop to generate multiple random numbers at once. You can also use the RANDOM_SEED subroutine to generate multiple random numbers with different ranges in a loop. This is useful for applications that require a large number of random numbers.

How can I ensure that the random numbers generated in Fortran 90 are truly random?

Fortran 90 uses a default random number generator that is based on the Mersenne Twister algorithm, which is a highly regarded and reliable random number generator. However, if you require more randomness, you can use the RANDOM_SEED subroutine to specify a different seed for the random number generator. Additionally, you can use a library such as the Numerical Recipes library to generate more complex and truly random numbers in Fortran 90.

Similar threads

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