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

Click For Summary
SUMMARY

The discussion focuses on generating a random number between 1 and 10 in Fortran 90. The provided code snippet utilizes the random_number function, which generates a number between 0 and 1. To achieve a range of 1 to 10, users must scale the output and apply a rounding function for integers. Additionally, initializing the random number generator's seed with CALL SEED(ii) is essential to produce different sequences of random numbers on each execution.

PREREQUISITES
  • Understanding of Fortran 90 syntax and structure
  • Familiarity with the random_number function
  • Knowledge of scaling and rounding functions in programming
  • Concept of seeding random number generators
NEXT STEPS
  • Learn how to implement scaling in Fortran 90 to adjust random number ranges
  • Explore rounding functions in Fortran 90 for converting real numbers to integers
  • Research the impact of different seed values on random number generation
  • Investigate advanced random number generation techniques in Fortran 90
USEFUL FOR

Programmers, especially those working with Fortran 90, and anyone interested in random number generation techniques for simulations or games.

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.
 

Similar threads

  • · Replies 4 ·
Replies
4
Views
3K
  • · Replies 4 ·
Replies
4
Views
3K
  • · Replies 20 ·
Replies
20
Views
3K
  • · Replies 5 ·
Replies
5
Views
2K
  • · Replies 5 ·
Replies
5
Views
2K
  • · Replies 5 ·
Replies
5
Views
5K
Replies
22
Views
5K
  • · Replies 12 ·
Replies
12
Views
3K
  • · Replies 5 ·
Replies
5
Views
2K
  • · Replies 2 ·
Replies
2
Views
2K