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.
 
Thread 'Star maps using Blender'
Blender just recently dropped a new version, 4.5(with 5.0 on the horizon), and within it was a new feature for which I immediately thought of a use for. The new feature was a .csv importer for Geometry nodes. Geometry nodes are a method of modelling that uses a node tree to create 3D models which offers more flexibility than straight modeling does. The .csv importer node allows you to bring in a .csv file and use the data in it to control aspects of your model. So for example, if you...
I tried a web search "the loss of programming ", and found an article saying that all aspects of writing, developing, and testing software programs will one day all be handled through artificial intelligence. One must wonder then, who is responsible. WHO is responsible for any problems, bugs, deficiencies, or whatever malfunctions which the programs make their users endure? Things may work wrong however the "wrong" happens. AI needs to fix the problems for the users. Any way to...

Similar threads

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