×
INTELLIGENT WORK FORUMS
FOR COMPUTER PROFESSIONALS

Are you a
Computer / IT professional?
Join Tek-Tips Forums!
• Talk With Other Members
• Be Notified Of Responses
• Keyword Search
Favorite Forums
• Automated Signatures
• Best Of All, It's Free!

*Tek-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

#### Posting Guidelines

Promoting, selling, recruiting, coursework and thesis posting is forbidden.

# (Pseudo) Random number generator help for a beginner

## (Pseudo) Random number generator help for a beginner

(OP)
Hello, it's been quite awhile since I was last programming in fortran and it was mostly for molecular modeling in chemistry.

I would like to ask for help with writing a random number generator. I would like it to generate 8 digits in hexadecimal system, this part I think I can do myself, but the problem is that I need every digit from that 8 random digits to be just a single number so not like this: (this is just an example)

AC98D
6374999
EF16383
1.72264576E-02

etc.

But just one number e.g.

A
9
D
2

etc.

I know that members of this forum are very advanced, nice and helpful people so I hope that there will be a good soul who would help me out.

I am using gcc (gfortran on a Mac).

This is what I've written so far:

program rand_test
implicit none
real a,b,c,d,e,f,g,h
integer seed

seed=time()

call srand(seed)
a=rand(0)
b=rand(0)
c=rand(0)
d=rand(0)
e=rand(0)
f=rand(0)
g=rand(0)
h=rand(0)
print *, "first 8 numbers:", a, b, c, d, e, f, g, h

end

### RE: (Pseudo) Random number generator help for a beginner

Hi Johny French,

I tried for example this:

#### CODE

program JohnyFrench
implicit none
real          :: r
integer       :: i, j

! initialize random seed
call init_random_seed()

do j=1, 8
! generate real random number
call random_number(r)

! create integer random number: 0, 1, .., 15
i = floor(16*r)

write(*,'(i2, a3, z1)') i, ' = ', i
end do

end program JohnyFrench

SUBROUTINE init_random_seed()
! see:
! https://gcc.gnu.org/onlinedocs/gcc-4.8.0/gfortran/RANDOM_005fSEED.html#RANDOM_005fSEED
INTEGER :: i, n, clock
INTEGER, DIMENSION(:), ALLOCATABLE :: seed

CALL RANDOM_SEED(size = n)
ALLOCATE(seed(n))

CALL SYSTEM_CLOCK(COUNT=clock)

seed = clock + 37 * (/ (i - 1, i = 1, n) /)
CALL RANDOM_SEED(PUT = seed)

DEALLOCATE(seed)
END SUBROUTINE 

or this (using arrays):

#### CODE

program JohnyFrench
implicit none
real          :: real_array(8)
integer       :: int_array(8), j

! initialize random seed
call init_random_seed()

! generate array of real random numbers
call random_number(real_array)

! create array of integer random numbers: 0, 1, .., 15
int_array = floor(16*real_array)

do j=1, 8
write(*,'(i2, a3, z1)') int_array(j), ' = ', int_array(j)
end do

end program JohnyFrench

SUBROUTINE init_random_seed()
! see:
! https://gcc.gnu.org/onlinedocs/gcc-4.8.0/gfortran/RANDOM_005fSEED.html#RANDOM_005fSEED
INTEGER :: i, n, clock
INTEGER, DIMENSION(:), ALLOCATABLE :: seed

CALL RANDOM_SEED(size = n)
ALLOCATE(seed(n))

CALL SYSTEM_CLOCK(COUNT=clock)

seed = clock + 37 * (/ (i - 1, i = 1, n) /)
CALL RANDOM_SEED(PUT = seed)

DEALLOCATE(seed)
END SUBROUTINE 

Compilation and running:

#### CODE

$gfortran JohnyFrench.f95 -o JohnyFrench$ ./JohnyFrench
5 = 5
15 = F
11 = B
15 = F
5 = 5
7 = 7
2 = 2
12 = C

$./JohnyFrench 0 = 0 12 = C 13 = D 10 = A 10 = A 11 = B 13 = D 12 = C$ ./JohnyFrench
1 = 1
12 = C
12 = C
1 = 1
8 = 8
8 = 8
14 = E
13 = D 

### RE: (Pseudo) Random number generator help for a beginner

(OP)
@Microm, thank you so much. I thought I’d be waiting for weeks for an answer. You can’t even imagine how grateful I am. You literally saved me from being fired from job. No kidding.

#### Red Flag This Post

Please let us know here why this post is inappropriate. Reasons such as off-topic, duplicates, flames, illegal, vulgar, or students posting their homework.

#### Red Flag Submitted

Thank you for helping keep Tek-Tips Forums free from inappropriate posts.
The Tek-Tips staff will check this out and take appropriate action.

Close Box

# Join Tek-Tips® Today!

Join your peers on the Internet's largest technical computer professional community.
It's easy to join and it's free.

Here's Why Members Love Tek-Tips Forums:

• Talk To Other Members
• Notification Of Responses To Questions
• Favorite Forums One Click Access
• Keyword Search Of All Posts, And More...

Register now while it's still free!