-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathmo_xor4096.f90
2212 lines (1849 loc) · 70.4 KB
/
mo_xor4096.f90
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
module mo_xor4096
! -----------------------------------------------------------------------------
! The original version of this source code (without multiple streams, optional
! arguments and gaussian distributed RN) is under GNU General Public Licence
! xorgens.c
! Copyright (C) 2004 R. P. Brent.
! This program is free software; you can redistribute it and/or
! modify it under the terms of the GNU General Public License,
! version 2, June 1991, as published by the Free Software Foundation.
! For details see http://www.gnu.org/copyleft/gpl.html .
! Author: Richard P. Brent (random@rpbrent.co.uk)
! -----------------------------------------------------------------------------
! Written Juliane Mai, Nov 2011
! License
! -------
! This file is part of the JAMS Fortran package, distributed under the MIT License.
!
! Copyright (c) 2011-2013 Juliane Mai
!
! Permission is hereby granted, free of charge, to any person obtaining a copy
! of this software and associated documentation files (the "Software"), to deal
! in the Software without restriction, including without limitation the rights
! to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
! copies of the Software, and to permit persons to whom the Software is
! furnished to do so, subject to the following conditions:
!
! The above copyright notice and this permission notice shall be included in all
! copies or substantial portions of the Software.
!
! THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
! IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
! FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
! AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
! LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
! OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
! SOFTWARE.
use mo_kind, only: i4, i8, sp, dp
Implicit NONE
PUBLIC :: n_save_state ! dimension of vector keeping the state of a stream
PUBLIC :: get_timeseed ! Returns a seed dependend on time
PUBLIC :: xor4096 ! Generates uniform distributed random number
PUBLIC :: xor4096g ! Generates gaussian distributed random number
! ------------------------------------------------------------------
! NAME
! get_timeseed
! PURPOSE
! Function which returns a scalar or a vector of integers
! dependent on time.
! This returned values can be used a seed for initializing
! random number generators like xor4096.
! If the return variable is a vector, only the first entry
! depends on time while the others are just the entry before
! increased by 1000.
! CALLING SEQUENCE
! call get_timeseed(seed)
! INTENT(IN)
! None
! INTENT(INOUT)
! integer(i4/i8) :: seed
! OR
! integer(i4/i8), dimension(:) :: seed
! INTENT(OUT)
! None
! INTENT(IN), OPTIONAL
! None
! INTENT(INOUT), OPTIONAL
! None
! INTENT(OUT), OPTIONAL
! None
! RESTRICTIONS
! None
! EXAMPLE
! integer(i4) :: seed
! call get_timeseed(seed)
! --> e.g. seed = 327_i4
! integer(i8), dimension(3) :: seed
! call get_timeseed(seed)
! --> e.g. seed = (/ 327_i8, 1327_i8, 2327_i8 /)
! LITERATURE
! HISTORY
! Written, Juliane Mai, Aug 2012
INTERFACE get_timeseed
MODULE PROCEDURE get_timeseed_i4_0d, get_timeseed_i4_1d, &
get_timeseed_i8_0d, get_timeseed_i8_1d
END INTERFACE get_timeseed
! ------------------------------------------------------------------
! NAME
! xor4096
! PURPOSE
! Generates a uniform distributed random number based on xor4096 algorithm proposed by
! Brent et.al (2006).
! ****************************************************************************************
! The original version of this source code (without multiple streams and
! optional arguments) is under GNU General Public Licence
! xorgens.c
! Copyright (C) 2004 R. P. Brent.
! This program is free software; you can redistribute it and/or
! modify it under the terms of the GNU General Public License,
! version 2, June 1991, as published by the Free Software Foundation.
! For details see http://www.gnu.org/copyleft/gpl.html .
! Author: Richard P. Brent (random@rpbrent.co.uk)
! ****************************************************************************************
! The period of the generator is
! (2^4096 - 1)*2^32 for single precision version and
! (2^4096 - 1)*2^64 for double precision version.
! The generator is based on bitwise XOR compositions of left- and right-shifted numbers.
! The generator has to be called once with a non-zero seed value which initializes a new
! random number stream. The subsequent calls are with seed=0 which returns the following
! numbers within the beforehand initialized stream. Since the random numbers are based on
! the initial seed, the precison of the seed (sp/dp) determines the precision of the
! returned random number (sp/dp).
! If one initialize the generator with an array of seeds, one initializes n independent
! streams of random numbers.
! Lets assume that the streams with seed 1_sp is 10, 20, 30 ...
! 2_sp is 40, 50, 60 ...
! 3_sp is 70, 80, 90 ...
! What you get is:
! 1st call
! call xor( (/ 1_SP, 2_SP, 3_SP /), RN ) --> RN = (/ 10, 40, 70 /)
! 2nd call
! call xor( (/ 0_SP, 0_SP, 0_SP /), RN ) --> RN = (/ 20, 50, 80 /)
! 3rd call
! call xor( (/ 0_SP, 0_SP, 0_SP /), RN ) --> RN = (/ 30, 60, 90 /)
! Since after every initialization one looses the old stream of random numbers,
! it might be necessary to switch between different streams. Therefore, one needs
! the optional save_state argument.
! What you get is:
! 1st call of 1st stream
! call xor( 1_SP, RN, save_state=save_stream_1 ) RN = 10
! 2nd call of 1st stream
! call xor( 0_SP, RN, save_state=save_stream_1 ) RN = 20
! 1st call of 2nd stream
! call xor( 2_SP, RN, save_state=save_stream_2 ) RN = 40
! 3rd call of 1st stream
! call xor( 0_SP, RN, save_state=save_stream_1 ) RN = 30
! Note: If you would have called 4 times without optional argument,
! you would have get 50 in the 4th call.
! CALLING SEQUENCE
! call xor4096(seed, rn)
! call xor4096(seed, rn, save_state=save_state)
! INTENT(IN)
! integer(i4/i8) :: seed/seed(:) value or 1D-array with non-zero seeds for
! initialization or zero for subsequent calls
! INTENT(INOUT)
! none
! INTENT(OUT)
! integer(i4/i8)/real(sp/dp) :: RN/RN(size(seed))
! uniform distributed random number with
! interval:
! i4: (-2^31,2^31-1)
! i8: (-2^63,2^63-1)
! sp: (0.0_sp, 1.0_sp)
! dp: (0.0_dp, 1.0_dp)
! INTENT(IN), OPTIONAL
! none
! INTENT(INOUT), OPTIONAL
! integer(i4/i8), dimension(size(seed), n_save_state) :: save_state
! array carrying state of random number stream
! this should be used if several streams are used, i.e.
! each stream has its own save_state
! INTENT(OUT), OPTIONAL
! None
! RESTRICTIONS
! The dimension of the optional argument can be set using the public module parameter n_save_state.
! If random numbers are in single precision (sp), one needs seed and save_state in (i4).
! If random numbers are in double precision (dp), one needs seed and save_state in (i8).
! EXAMPLE
! I4 seeds are generating I4 or SP random numbers
! I8 seeds are generating I8 or DP random numbers
!
! ! Initializing
! real(SP) :: RN(3)
! seed = (/ 1_I4, 100_I4, 2_I4 /)
! call xor4096(seed,RN)
! print*, RN --> (/ 0.1_sp, 0.2_sp, 0.6_sp /)
!
! ! proper usage after initialization
! seed = (/ 0_I4, 0_I4, 0_I4 /)
! call xor4096(seed,RN)
! print*, RN --> (/ 0.3_sp, 0.1_sp, 0.5_sp /)
!
! -> see also example in test_mo_xor4096 directory
! LITERATURE
! Brent RP - Some long-period random number generators using shifts and xors, 2010
! Brent RP - From Mersenne Primes to Rndom Number Generators, 2006
! L''Ecuyer P & Simard R - ACM: TestU01: A C Library for Empirical Testing of
! Random Number Generators, 2007
! HISTORY
! Written, Juliane Mai, Nov 2011
INTERFACE xor4096
MODULE PROCEDURE xor4096s_0d, xor4096s_1d, xor4096f_0d, xor4096f_1d, &
xor4096l_0d, xor4096l_1d, xor4096d_0d, xor4096d_1d
END INTERFACE xor4096
! ------------------------------------------------------------------
! NAME
! xor4096g
! PURPOSE
! Generates a gaussian distributed random number. First, a uniform distributed random
! number based on xor4096 algorithm is generated. Second, this number is transformed
! using the Polar method of Box-Mueller-transform to calculate the gaussian distributed
! numbers.
! ****************************************************************************************
! The original version of this source code (without multiple streams,
! optional arguments and gaussian distributed RN) is under GNU General Public Licence
! xorgens.c
! Copyright (C) 2004 R. P. Brent.
! This program is free software; you can redistribute it and/or
! modify it under the terms of the GNU General Public License,
! version 2, June 1991, as published by the Free Software Foundation.
! For details see http://www.gnu.org/copyleft/gpl.html .
! Author: Richard P. Brent (random@rpbrent.co.uk)
! ****************************************************************************************
! The generator has to be called once with a non-zero seed value which initializes a new
! random number stream. The subsequent calls are with seed=0 which returns the following
! numbers within the beforehand initialized stream. Since the random numbers are based on
! the initial seed, the precison of the seed (sp/dp) determines the precision of the
! returned random number (sp/dp).
! The returned values are gaussian distributed with mean 0 and variance 1.
! The polar method of the Box-Mueller transform transforms two uniform distributed
! random numbers u1 and u2 into two gaussian distributed random numbers x1 and x2.
! First, two uniform numbers a1, a2 distributed between (-1,1) are calculated:
! a1 = 2u1 - 1
! a2 = 2u2 - 1
! Second, q = a1^2 + a2^2 is calculated. If (q = 0) or (q > 1) one has to generate
! a new x1 and x2, since a divison by q is needed afterwards and q needs to be distributed
! within the unit circle.
! Third,
! p = Sqrt( -2 LN(q) / q )
! is calculated.
! The numbers z1 and z2 with
! z1 = a1 * p
! z2 = a2 * p
! are then gaussian distributed with mean 0 and variance 1.
! CALLING SEQUENCE
! call xor4096g(seed, rn) or
! call xor4096g(seed, rn, save_state=save_state)
! INTENT(IN)
! integer(i4/i8) :: seed/seed(:) value or 1D-array with non-zero seeds for
! initialization or zero for subsequent calls
! INTENT(INOUT)
! none
! INTENT(OUT)
! real(sp/dp) :: RN/RN(size(seed))
! gaussian distributed random number with
! interval:
! sp: RN ~ N(0.0_sp, 1.0_sp)
! dp: RN ~ N(0.0_dp, 1.0_dp)
! INTENT(IN), OPTIONAL
! none
! INTENT(INOUT), OPTIONAL
! integer(i4/i8), dimension(size(seed),n_save_state) :: save_state
! INTENT(OUT), OPTIONAL
! None
! RESTRICTIONS
! The dimension of the optional argument can be set using the public module parameter n_save_state.
! If random numbers are in single precision (sp), one needs seed and save_state in (i4).
! If random numbers are in double precision (dp), one needs seed and save_state in (i8).
! EXAMPLE
! I4 seeds are generating I4 or SP random numbers
! I8 seeds are generating I8 or DP random numbers
!
! ! Initializing
! real(SP) :: RN(3)
! seed = (/ 1_I4, 100_I4, 2_I4 /)
! call xor4096(seed,RN)
! print*, RN --> (/ 0.1_sp, -0.2_sp, 0.3_sp /)
!
! ! proper usage after initialization
! seed = (/ 0_I4, 0_I4, 0_I4 /)
! call xor4096(seed,RN)
! print*, RN --> (/ 0.2_sp, 0.1_sp, -0.1_sp /)
!
! -> see also example in test_mo_xor4096 directory
! LITERATURE
! Brent RP - Some long-period random number generators using shifts and xors, 2010
! Brent RP - From Mersenne Primes to Rndom Number Generators, 2006
! L''Ecuyer P & Simard R - ACM: TestU01: A C Library for Empirical Testing of
! Random Number Generators, 2007
! http://en.wikipedia.org/wiki/Marsaglia_polar_method
! http://de.wikipedia.org/wiki/Polar-Methode
! HISTORY
! Written, Juliane Mai, Nov 2011
! Modified, Juliane Mai, Feb 2013 - all optionals combined in save_state
INTERFACE xor4096g
MODULE PROCEDURE xor4096gf_0d, xor4096gf_1d, xor4096gd_0d, xor4096gd_1d
END INTERFACE xor4096g
! ------------------------------------------------------------------
PRIVATE
!> Dimension of vector saving the state of a stream
integer(i4), parameter :: n_save_state = 132_i4
! ------------------------------------------------------------------
CONTAINS
! ------------------------------------------------------------------
subroutine get_timeseed_i4_0d(seed)
implicit none
integer(i4), intent(inout) :: seed
! local variables
integer(i4), dimension(8) :: time_array
call date_and_time(values=time_array)
seed = &
time_array(5) * 3600000_i4 + & ! hour
time_array(6) * 60000_i4 + & ! minutes
time_array(7) * 1000_i4 + & ! seconds
time_array(8) * 1_i4 ! milliseconds
end subroutine get_timeseed_i4_0d
subroutine get_timeseed_i4_1d(seed)
implicit none
integer(i4), dimension(:), intent(inout) :: seed
! local variables
integer(i4), dimension(8) :: time_array
integer(i4) :: i
call date_and_time(values=time_array)
seed(1) = &
time_array(5) * 3600000_i4 + & ! hour
time_array(6) * 60000_i4 + & ! minutes
time_array(7) * 1000_i4 + & ! seconds
time_array(8) * 1_i4 ! milliseconds
do i=2,size(seed)
seed(i) = seed(i-1) + 1000_i4
end do
end subroutine get_timeseed_i4_1d
subroutine get_timeseed_i8_0d(seed)
implicit none
integer(i8), intent(inout) :: seed
! local variables
integer(i4), dimension(8) :: time_array
call date_and_time(values=time_array)
seed = &
int(time_array(5),i8) * 3600000_i8 + & ! hour
int(time_array(6),i8) * 60000_i8 + & ! minutes
int(time_array(7),i8) * 1000_i8 + & ! seconds
int(time_array(8),i8) * 1_i8 ! milliseconds
end subroutine get_timeseed_i8_0d
subroutine get_timeseed_i8_1d(seed)
implicit none
integer(i8), dimension(:), intent(inout) :: seed
! local variables
integer(i4), dimension(8) :: time_array
integer(i4) :: i
call date_and_time(values=time_array)
seed(1) = &
int(time_array(5),i8) * 3600000_i8 + & ! hour
int(time_array(6),i8) * 60000_i8 + & ! minutes
int(time_array(7),i8) * 1000_i8 + & ! seconds
int(time_array(8),i8) * 1_i8 ! milliseconds
do i=2,size(seed)
seed(i) = seed(i-1) + 1000_i8
end do
end subroutine get_timeseed_i8_1d
! ------------------------------------------------------------------
subroutine xor4096s_0d(seed,SingleIntegerRN,save_state)
implicit none
integer(i4), intent(in) :: seed
integer(i4), intent(out) :: SingleIntegerRN
integer(i4), optional, dimension(n_save_state), intent(inout) :: save_state
integer(i4) :: wlen, r, s, a, b, c, d
integer(i4), save :: w
integer(i4), save :: x(0:127) ! x(0) ... x(r-1)
integer(i4) :: weyl = 1640531527_i4 !Z'61C88647' ! Hexadecimal notation
integer(i4) :: t, v, tmp
integer(i4), save :: i = -1 ! i<0 indicates first call
integer(i4) :: k
!$omp threadprivate(x,i,w)
wlen = 32
r = 128
s = 95
a = 17
b = 12
c = 13
d = 15
if ( present(save_state) .and. (seed .eq. 0) ) then
x(0:r-1) = save_state(1:r)
i = save_state(r+1)
w = save_state(r+2)
end if
If ((i .lt. 0) .or. (seed .ne. 0)) then ! Initialization necessary
If (seed .ne. 0) then ! v must be nonzero
v = seed
else
v = NOT(seed)
end if
do k=wlen,1,-1 ! Avoid correlations for close seeds
! This recurrence has period of 2^32-1
v = IEOR(v,ISHFT(v,13))
v = IEOR(v,ISHFT(v,-17))
v = IEOR(v,ISHFT(v, 5))
end do
! Initialize circular array
w = v
do k=0,r-1
! w = w + weyl
if (w < 0_i4) then
w = w + weyl
else if ((huge(1_i4) - w) > weyl) then
w = w + weyl
else
tmp = -(huge(1_i4) - w - weyl)
w = tmp - huge(1_i4) - 2_i4
endif
v = IEOR(v,ISHFT(v,13))
v = IEOR(v,ISHFT(v,-17))
v = IEOR(v,ISHFT(v, 5))
x(k) = v + w
end do
! Discard first 4*r results (Gimeno)
i = r-1
do k = 4*r,1,-1
i = IAND(i+1,r-1)
t = x(i)
v = x(IAND(i+(r-s),r-1))
t = IEOR(t,ISHFT(t,a))
t = IEOR(t,ISHFT(t,-b))
v = IEOR(v,ISHFT(v,c))
v = IEOR(v,IEOR(t,ISHFT(v,-d)))
x(i) = v
end do
end if ! end of initialization
! Apart from initialization (above), this is the generator
i = IAND(i+1,r-1)
t = x(i)
v = x(IAND(i+(r-s),r-1))
t = IEOR(t,ISHFT(t,a))
t = IEOR(t,ISHFT(t,-b))
v = IEOR(v,ISHFT(v,c))
v = IEOR(v,IEOR(t,ISHFT(v,-d)))
x(i) = v
w = w + weyl
SingleIntegerRN = v+w
if( present(save_state) ) then
save_state(1:r) = x(0:r-1)
save_state(r+1) = i
save_state(r+2) = w
if ((r+3) <= n_save_state) save_state(r+3:n_save_state) = 0
end if
end subroutine xor4096s_0d
! -----------------------------------------------------------------------------
subroutine xor4096s_1d(seed,SingleIntegerRN,save_state)
implicit none
integer(i4), dimension(:), intent(in) :: seed
integer(i4), dimension(size(seed,1)), intent(out) :: SingleIntegerRN
integer(i4), optional, dimension(size(seed,1),n_save_state), intent(inout) :: save_state
integer(i4) :: m
integer(i4) :: wlen, r, s, a, b, c, d
integer(i4) :: weyl = 1640531527_i4 !Z'61C88647' ! Hexadecimal notation
integer(i4) :: k, j, tmp
integer(i4), dimension(size(seed,1)) :: t,v
integer(i4), dimension(:,:), allocatable, save :: x ! x(0) ... x(r-1)
integer(i4), dimension(:), allocatable, save :: i,w ! i<0 indicates first call
!$omp threadprivate(x,i,w)
wlen = 32
r = 128
s = 95
a = 17
b = 12
c = 13
d = 15
m = size(seed,1)
if (any(seed .eq. 0_i4) .and. any(seed .ne. 0_i4)) then
stop 'xor4096: seeds have to be eigther all 0 or all larger than 0'
end if
if ( present(save_state) .and. all(seed .eq. 0_i4) ) then
if (allocated(x)) then
if (size(x,1) .ne. m) then
deallocate(x)
deallocate(i)
deallocate(w)
allocate(i(m))
allocate(x(m,0:r-1))
allocate(w(m))
end if
end if
if (.not. allocated(x) ) then
allocate(i(m))
allocate(x(m,0:r-1))
allocate(w(m))
end if
x(:,0:r-1) = save_state(:,1:r)
i(:) = save_state(:,r+1)
w(:) = save_state(:,r+2)
end if
if(all(seed .ne. 0_i4)) then
if ( allocated(x) ) then
deallocate(x)
deallocate(i)
deallocate(w)
end if
allocate(i(m))
i = -1
allocate(x(m,0:r-1))
allocate(w(m))
end if
Do j = 1, m
If ((i(j) .lt. 0) .or. (seed(j) .ne. 0)) then ! Initialization necessary
If (seed(j) .ne. 0) then ! v must be nonzero
v(j) = seed(j)
else
v(j) = NOT(seed(j))
end if
do k=wlen,1,-1 ! Avoid correlations for close seeds
! This recurrence has period of 2^32-1
v(j) = IEOR(v(j),ISHFT(v(j),13))
v(j) = IEOR(v(j),ISHFT(v(j),-17))
v(j) = IEOR(v(j),ISHFT(v(j), 5))
end do
! Initialize circular array
w(j) = v(j)
do k=0,r-1
! w(j) = w(j) + weyl
if (w(j) < 0_i4) then
w(j) = w(j) + weyl
else if ((huge(1_i4) - w(j)) > weyl) then
w(j) = w(j) + weyl
else
tmp = -(huge(1_i4) - w(j) - weyl)
w(j) = tmp - huge(1_i4) - 2_i4
endif
v(j) = IEOR(v(j),ISHFT(v(j),13))
v(j) = IEOR(v(j),ISHFT(v(j),-17))
v(j) = IEOR(v(j),ISHFT(v(j), 5))
x(j,k) = v(j) + w(j)
end do
! Discard first 4*r results (Gimeno)
i(j) = r-1
do k = 4*r,1,-1
i(j) = IAND(i(j)+1,r-1)
t(j) = x(j,i(j))
v(j) = x(j,IAND(i(j)+(r-s),r-1))
t(j) = IEOR(t(j),ISHFT(t(j),a))
t(j) = IEOR(t(j),ISHFT(t(j),-b))
v(j) = IEOR(v(j),ISHFT(v(j),c))
v(j) = IEOR(v(j),IEOR(t(j),ISHFT(v(j),-d)))
x(j,i(j)) = v(j)
end do
end if ! end of initialization
end do
! Apart from initialization (above), this is the generator
do j=1,m
i(j) = IAND(i(j)+1,r-1)
t(j) = x(j,i(j))
v(j) = x(j,IAND(i(j)+(r-s),r-1))
t(j) = IEOR(t(j),ISHFT(t(j),a))
t(j) = IEOR(t(j),ISHFT(t(j),-b))
v(j) = IEOR(v(j),ISHFT(v(j),c))
v(j) = IEOR(v(j),IEOR(t(j),ISHFT(v(j),-d)))
x(j,i(j)) = v(j)
w(j) = w(j) + weyl
end do
SingleIntegerRN = v+w
if( present(save_state) ) then
save_state(:,1:r) = x(:,0:r-1)
save_state(:,r+1) = i(:)
save_state(:,r+2) = w(:)
if ((r+3) <= n_save_state) save_state(:,r+3:n_save_state) = 0
end if
end subroutine xor4096s_1d
! -----------------------------------------------------------------------------
subroutine xor4096f_0d(seed,SingleRealRN,save_state)
implicit none
integer(i4), intent(in) :: seed
real(SP), intent(out) :: SingleRealRN
integer(i4), optional, dimension(n_save_state), intent(inout) :: save_state
integer(i4) :: wlen, r, s, a, b, c, d
integer(i4), save :: w
integer(i4), save :: x(0:127) ! x(0) ... x(r-1)
integer(i4) :: weyl = 1640531527_i4 !Z'61C88647' ! Hexadecimal notation
integer(i4) :: t, v, tmp
integer(i4), save :: i = -1 ! i<0 indicates first call
integer(i4) :: k
real(SP) :: t24 = 1.0_SP/16777216.0_SP ! = 0.5^24 = 1/2^24
!$omp threadprivate(x,i,w)
! produces a 24bit Integer Random Number (0...16777216) and
! scales it afterwards to (0.0,1.0)
wlen = 32
r = 128
s = 95
a = 17
b = 12
c = 13
d = 15
if ( present(save_state) .and. (seed .eq. 0) ) then
x(0:r-1) = save_state(1:r)
i = save_state(r+1)
w = save_state(r+2)
end if
If ((i .lt. 0) .or. (seed .ne. 0)) then ! Initialization necessary
If (seed .ne. 0) then ! v must be nonzero
v = seed
else
v = NOT(seed)
end if
do k=wlen,1,-1 ! Avoid correlations for close seeds
! This recurrence has period of 2^32-1
v = IEOR(v,ISHFT(v,13))
v = IEOR(v,ISHFT(v,-17))
v = IEOR(v,ISHFT(v, 5))
end do
! Initialize circular array
w = v
do k=0,r-1
! w = w + weyl
if (w < 0_i4) then
w = w + weyl
else if ((huge(1_i4) - w) > weyl) then
w = w + weyl
else
tmp = -(huge(1_i4) - w - weyl)
w = tmp - huge(1_i4) - 2_i4
endif
v = IEOR(v,ISHFT(v,13))
v = IEOR(v,ISHFT(v,-17))
v = IEOR(v,ISHFT(v, 5))
x(k) = v + w
end do
! Discard first 4*r results (Gimeno)
i = r-1
do k = 4*r,1,-1
i = IAND(i+1,r-1)
t = x(i)
v = x(IAND(i+(r-s),r-1))
t = IEOR(t,ISHFT(t,a))
t = IEOR(t,ISHFT(t,-b))
v = IEOR(v,ISHFT(v,c))
v = IEOR(v,IEOR(t,ISHFT(v,-d)))
x(i) = v
end do
end if ! end of initialization
! Apart from initialization (above), this is the generator
v = 0_i4
Do While (v .eq. 0_i4)
i = IAND(i+1,r-1)
t = x(i)
v = x(IAND(i+(r-s),r-1))
t = IEOR(t,ISHFT(t,a))
t = IEOR(t,ISHFT(t,-b))
v = IEOR(v,ISHFT(v,c))
v = IEOR(v,IEOR(t,ISHFT(v,-d)))
x(i) = v
w = w + weyl
v = v + w
v = ISHFT(v,-8)
End Do
SingleRealRN = t24*v
if( present(save_state) ) then
save_state(1:r) = x(0:r-1)
save_state(r+1) = i
save_state(r+2) = w
if ((r+3) <= n_save_state) save_state(r+3:n_save_state) = 0
end if
end subroutine xor4096f_0d
! -----------------------------------------------------------------------------
subroutine xor4096f_1d(seed,SingleRealRN,save_state)
implicit none
integer(i4), dimension(:), intent(in) :: seed
real(SP), dimension(size(seed)), intent(out) :: SingleRealRN
integer(i4), optional, dimension(size(seed,1),n_save_state), intent(inout) :: save_state
integer(i4) :: m
integer(i4) :: wlen, r, s, a, b, c, d
integer(i4) :: weyl = 1640531527_i4 !Z'61C88647' = Hexadecimal notation
integer(i4) :: k, j, tmp
real(SP), save :: t24 = 1.0_SP/16777216.0_SP ! = 0.5^24 = 1/2^24
integer(i4), dimension(size(seed)) :: t,v
integer(i4), dimension(:,:), allocatable, save :: x ! x(0) ... x(r-1)
integer(i4), dimension(:), allocatable, save :: i,w ! i<0 indicates first call
! produces a 24bit Integer Random Number (0...16777216) and
! scales it afterwards to (0.0,1.0)
!$omp threadprivate(x,i,w)
wlen = 32
r = 128
s = 95
a = 17
b = 12
c = 13
d = 15
m = size(seed,1)
if (any(seed .eq. 0_i4) .and. any(seed .ne. 0_i4)) then
stop 'xor4096: seeds have to be eigther all 0 or all larger than 0'
end if
if ( present(save_state) .and. all(seed .eq. 0_i4) ) then
if (allocated(x)) then
if (size(x,1) .ne. m) then
deallocate(x)
deallocate(i)
deallocate(w)
allocate(i(m))
allocate(x(m,0:r-1))
allocate(w(m))
end if
end if
if (.not. allocated(x) ) then
allocate(i(m))
allocate(x(m,0:r-1))
allocate(w(m))
end if
x(:,0:r-1) = save_state(:,1:r)
i(:) = save_state(:,r+1)
w(:) = save_state(:,r+2)
end if
if(all(seed .ne. 0_i4)) then
if ( allocated(x) ) then
deallocate(x)
deallocate(i)
deallocate(w)
end if
allocate(i(m))
i = -1
allocate(x(m,0:r-1))
allocate(w(m))
end if
Do j = 1,m !Loop over every stream
If ((i(j) .lt. 0) .or. (seed(j) .ne. 0)) then ! Initialization necessary
If (seed(j) .ne. 0) then ! v must be nonzero
v(j) = seed(j)
else
v(j) = NOT(seed(j))
end if
do k=wlen,1,-1 ! Avoid correlations for close seeds
! This recurrence has period of 2^32-1
v(j) = IEOR(v(j),ISHFT(v(j),13))
v(j) = IEOR(v(j),ISHFT(v(j),-17))
v(j) = IEOR(v(j),ISHFT(v(j), 5))
end do
! Initialize circular array
w(j) = v(j)
do k=0,r-1
! w(j) = w(j) + weyl
if (w(j) < 0_i4) then
w(j) = w(j) + weyl
else if ((huge(1_i4) - w(j)) > weyl) then
w(j) = w(j) + weyl
else
tmp = -(huge(1_i4) - w(j) - weyl)
w(j) = tmp - huge(1_i4) - 2_i4
endif
v(j) = IEOR(v(j),ISHFT(v(j),13))
v(j) = IEOR(v(j),ISHFT(v(j),-17))
v(j) = IEOR(v(j),ISHFT(v(j), 5))
x(j,k) = v(j) + w(j)
end do
! Discard first 4*r results (Gimeno)
i(j) = r-1
do k = 4*r,1,-1
i(j) = IAND(i(j)+1,r-1)
t(j) = x(j,i(j))
v(j) = x(j,IAND(i(j)+(r-s),r-1))
t(j) = IEOR(t(j),ISHFT(t(j),a))
t(j) = IEOR(t(j),ISHFT(t(j),-b))
v(j) = IEOR(v(j),ISHFT(v(j),c))
v(j) = IEOR(v(j),IEOR(t(j),ISHFT(v(j),-d)))
x(j,i(j)) = v(j)
end do
end if ! end of initialization
end do
! Apart from initialization (above), this is the generator
v = 0_i4
Do j=1,m
Do While (v(j) .eq. 0_i4)
i(j) = IAND(i(j)+1,r-1)
t(j) = x(j,i(j))
v(j) = x(j,IAND(i(j)+(r-s),r-1))
t(j) = IEOR(t(j),ISHFT(t(j),a))
t(j) = IEOR(t(j),ISHFT(t(j),-b))
v(j) = IEOR(v(j),ISHFT(v(j),c))
v(j) = IEOR(v(j),IEOR(t(j),ISHFT(v(j),-d)))
x(j,i(j)) = v(j)
w(j) = w(j) + weyl
v(j) = v(j) + w(j)
v(j) = ISHFT(v(j),-8)
End Do
End Do
SingleRealRN = t24*v
if( present(save_state) ) then
save_state(:,1:r) = x(:,0:r-1)
save_state(:,r+1) = i(:)
save_state(:,r+2) = w(:)
if ((r+3) <= n_save_state) save_state(:,r+3:n_save_state) = 0
end if
end subroutine xor4096f_1d
! -----------------------------------------------------------------------------
subroutine xor4096l_0d(seed,DoubleIntegerRN,save_state)
implicit none
integer(i8), intent(in) :: seed
integer(i8), intent(out) :: DoubleIntegerRN
integer(i8), optional, dimension(n_save_state), intent(inout) :: save_state
integer(i8) :: wlen, r, s, a, b, c, d
integer(i8), save :: w
integer(i8), save :: x(0:63) ! x(0) ... x(r-1)
integer(i8) :: weyl = 7046029254386353131_i8
integer(i8) :: t, v, tmp
integer(i8), save :: i = -1 ! i<0 indicates first call
integer(i8) :: k
!$omp threadprivate(x,i,w)
wlen = 64_i8
r = 64_i8
s = 53_i8
a = 33_i8
b = 26_i8
c = 27_i8
d = 29_i8
if ( present(save_state) .and. (seed .eq. 0_i8) ) then
x(0:r-1) = save_state(1:r)
i = save_state(r+1)
w = save_state(r+2)
end if
If ((i .lt. 0) .or. (seed .ne. 0)) then ! Initialization necessary
If (seed .ne. 0) then ! v must be nonzero
v = seed
else
v = NOT(seed)
end if
do k=wlen,1,-1 ! Avoid correlations for close seeds
! This recurrence has period of 2^64-1
v = IEOR(v,ISHFT(v,7))
v = IEOR(v,ISHFT(v,-9))
end do
! Initialize circular array
w = v
do k=0,r-1
! w = w + weyl
if (w < 0_i8) then
w = w + weyl