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 had a Microsoft Technical interview this past Friday, the question I was asked was this : How do you find the middle value for a dataset that is too big to fit in RAM? I was not able to figure this out during the interview, but I have been look in this all weekend and I read something online that said it can be done at O(N) using something called the counting sort histogram algorithm ( I did not learn that in my advanced data structures and algorithms class). I have watched some youtube...

Similar threads

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