Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

(Pseudo) Random number generator help for a beginner 1

Status
Not open for further replies.

Jonny French

Technical User
Sep 7, 2018
2
CZ
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

 
Hi Johny French,

I tried for example this:

Code:
[COLOR=#800080]program[/color] JohnyFrench
  [COLOR=#2e8b57][b]implicit[/b][/color] [COLOR=#2e8b57][b]none[/b][/color]
[COLOR=#2e8b57][b]  real[/b][/color]          :: r
  [COLOR=#2e8b57][b]integer[/b][/color]       :: i, j

  [COLOR=#0000ff]! initialize random seed[/color]
  [COLOR=#008b8b]call[/color] init_random_seed()

  [COLOR=#a52a2a][b]do[/b][/color] j[COLOR=#a52a2a][b]=[/b][/color][COLOR=#ff00ff]1[/color], [COLOR=#ff00ff]8[/color]
    [COLOR=#0000ff]! generate real random number[/color]
    [COLOR=#008b8b]call[/color] [COLOR=#008b8b]random_number[/color](r)

    [COLOR=#0000ff]! create integer random number: 0, 1, .., 15[/color]
    i [COLOR=#a52a2a][b]=[/b][/color] [COLOR=#008b8b]floor[/color]([COLOR=#ff00ff]16[/color][COLOR=#a52a2a][b]*[/b][/color]r)

    [COLOR=#a52a2a][b]write[/b][/color]([COLOR=#a52a2a][b]*[/b][/color],[COLOR=#ff00ff]'(i2, a3, z1)'[/color]) i, [COLOR=#ff00ff]' = '[/color], i
  [COLOR=#a52a2a][b]end do[/b][/color]

[COLOR=#800080]end program[/color] JohnyFrench

[COLOR=#800080]SUBROUTINE[/color] init_random_seed()
  [COLOR=#0000ff]! see:[/color]
  [COLOR=#0000ff]! [URL unfurl="true"]https://gcc.gnu.org/onlinedocs/gcc-4.8.0/gfortran/RANDOM_005fSEED.html#RANDOM_005fSEED[/URL][/color]
  [COLOR=#2e8b57][b]INTEGER[/b][/color] :: i, n, clock
  [COLOR=#2e8b57][b]INTEGER[/b][/color], [COLOR=#2e8b57][b]DIMENSION[/b][/color](:), [COLOR=#2e8b57][b]ALLOCATABLE[/b][/color] :: seed
          
  [COLOR=#008b8b]CALL[/color] [COLOR=#008b8b]RANDOM_SEED[/color]([COLOR=#008b8b]size[/color] [COLOR=#a52a2a][b]=[/b][/color] n)
  [COLOR=#a52a2a][b]ALLOCATE[/b][/color](seed(n))
          
  [COLOR=#008b8b]CALL[/color] [COLOR=#008b8b]SYSTEM_CLOCK[/color]([COLOR=#008b8b]COUNT[/color][COLOR=#a52a2a][b]=[/b][/color]clock)
          
  seed [COLOR=#a52a2a][b]=[/b][/color] clock [COLOR=#a52a2a][b]+[/b][/color] [COLOR=#ff00ff]37[/color] [COLOR=#a52a2a][b]*[/b][/color] ([COLOR=#a52a2a][b]/[/b][/color] (i [COLOR=#a52a2a][b]-[/b][/color] [COLOR=#ff00ff]1[/color], i [COLOR=#a52a2a][b]=[/b][/color] [COLOR=#ff00ff]1[/color], n) [COLOR=#a52a2a][b]/[/b][/color])
  [COLOR=#008b8b]CALL[/color] [COLOR=#008b8b]RANDOM_SEED[/color](PUT [COLOR=#a52a2a][b]=[/b][/color] seed)
          
  [COLOR=#a52a2a][b]DEALLOCATE[/b][/color](seed)
[COLOR=#800080]END SUBROUTINE[/color]

or this (using arrays):

Code:
[COLOR=#800080]program[/color] JohnyFrench
  [COLOR=#2e8b57][b]implicit[/b][/color] [COLOR=#2e8b57][b]none[/b][/color]
[COLOR=#2e8b57][b]  real[/b][/color]          :: real_array([COLOR=#ff00ff]8[/color])
  [COLOR=#2e8b57][b]integer[/b][/color]       :: int_array([COLOR=#ff00ff]8[/color]), j
  

  [COLOR=#0000ff]! initialize random seed[/color]
  [COLOR=#008b8b]call[/color] init_random_seed()

  [COLOR=#0000ff]! generate array of real random numbers[/color]
  [COLOR=#008b8b]call[/color] [COLOR=#008b8b]random_number[/color](real_array)
 
  [COLOR=#0000ff]! create array of integer random numbers: 0, 1, .., 15 [/color]
  int_array [COLOR=#a52a2a][b]=[/b][/color] [COLOR=#008b8b]floor[/color]([COLOR=#ff00ff]16[/color][COLOR=#a52a2a][b]*[/b][/color]real_array)

  [COLOR=#a52a2a][b]do[/b][/color] j[COLOR=#a52a2a][b]=[/b][/color][COLOR=#ff00ff]1[/color], [COLOR=#ff00ff]8[/color]
    [COLOR=#a52a2a][b]write[/b][/color]([COLOR=#a52a2a][b]*[/b][/color],[COLOR=#ff00ff]'(i2, a3, z1)'[/color]) int_array(j), [COLOR=#ff00ff]' = '[/color], int_array(j)
  [COLOR=#a52a2a][b]end do[/b][/color]

[COLOR=#800080]end program[/color] JohnyFrench

[COLOR=#800080]SUBROUTINE[/color] init_random_seed()
  [COLOR=#0000ff]! see:[/color]
  [COLOR=#0000ff]! [URL unfurl="true"]https://gcc.gnu.org/onlinedocs/gcc-4.8.0/gfortran/RANDOM_005fSEED.html#RANDOM_005fSEED[/URL][/color]
  [COLOR=#2e8b57][b]INTEGER[/b][/color] :: i, n, clock
  [COLOR=#2e8b57][b]INTEGER[/b][/color], [COLOR=#2e8b57][b]DIMENSION[/b][/color](:), [COLOR=#2e8b57][b]ALLOCATABLE[/b][/color] :: seed
          
  [COLOR=#008b8b]CALL[/color] [COLOR=#008b8b]RANDOM_SEED[/color]([COLOR=#008b8b]size[/color] [COLOR=#a52a2a][b]=[/b][/color] n)
  [COLOR=#a52a2a][b]ALLOCATE[/b][/color](seed(n))
          
  [COLOR=#008b8b]CALL[/color] [COLOR=#008b8b]SYSTEM_CLOCK[/color]([COLOR=#008b8b]COUNT[/color][COLOR=#a52a2a][b]=[/b][/color]clock)
          
  seed [COLOR=#a52a2a][b]=[/b][/color] clock [COLOR=#a52a2a][b]+[/b][/color] [COLOR=#ff00ff]37[/color] [COLOR=#a52a2a][b]*[/b][/color] ([COLOR=#a52a2a][b]/[/b][/color] (i [COLOR=#a52a2a][b]-[/b][/color] [COLOR=#ff00ff]1[/color], i [COLOR=#a52a2a][b]=[/b][/color] [COLOR=#ff00ff]1[/color], n) [COLOR=#a52a2a][b]/[/b][/color])
  [COLOR=#008b8b]CALL[/color] [COLOR=#008b8b]RANDOM_SEED[/color](PUT [COLOR=#a52a2a][b]=[/b][/color] seed)
          
  [COLOR=#a52a2a][b]DEALLOCATE[/b][/color](seed)
[COLOR=#800080]END SUBROUTINE[/color]

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
 
@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.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top