5
5
#
6
6
# ##########################################################
7
7
8
- using Random: Sampler, Random. GLOBAL_RNG
8
+ using Random: Sampler
9
+
10
+ if VERSION < v " 1.3.0-DEV.565"
11
+ default_rng () = Random. GLOBAL_RNG
12
+ else
13
+ using Random: default_rng
14
+ end
9
15
10
16
# ## Algorithms for sampling with replacement
11
17
@@ -25,7 +31,7 @@ function direct_sample!(rng::AbstractRNG, a::UnitRange, x::AbstractArray)
25
31
end
26
32
return x
27
33
end
28
- direct_sample! (a:: UnitRange , x:: AbstractArray ) = direct_sample! (Random . GLOBAL_RNG , a, x)
34
+ direct_sample! (a:: UnitRange , x:: AbstractArray ) = direct_sample! (default_rng () , a, x)
29
35
30
36
"""
31
37
direct_sample!([rng], a::AbstractArray, x::AbstractArray)
@@ -46,7 +52,7 @@ function direct_sample!(rng::AbstractRNG, a::AbstractArray, x::AbstractArray)
46
52
end
47
53
return x
48
54
end
49
- direct_sample! (a:: AbstractArray , x:: AbstractArray ) = direct_sample! (Random . GLOBAL_RNG , a, x)
55
+ direct_sample! (a:: AbstractArray , x:: AbstractArray ) = direct_sample! (default_rng () , a, x)
50
56
51
57
# check whether we can use T to store indices 1:n exactly, and
52
58
# use some heuristics to decide whether it is beneficial for k samples
@@ -103,28 +109,28 @@ sample_ordered!(sampler!, rng::AbstractRNG, a::AbstractArray,
103
109
Draw a pair of distinct integers between 1 and `n` without replacement.
104
110
105
111
Optionally specify a random number generator `rng` as the first argument
106
- (defaults to `Random.GLOBAL_RNG`).
112
+ (defaults to `Random.$( VERSION < v " 1.3 " ? " GLOBAL_RNG" : " default_rng() " ) `).
107
113
"""
108
114
function samplepair (rng:: AbstractRNG , n:: Integer )
109
115
i1 = rand (rng, one (n): n)
110
116
i2 = rand (rng, one (n): (n - one (n)))
111
117
return (i1, ifelse (i2 == i1, n, i2))
112
118
end
113
- samplepair (n:: Integer ) = samplepair (Random . GLOBAL_RNG , n)
119
+ samplepair (n:: Integer ) = samplepair (default_rng () , n)
114
120
115
121
"""
116
122
samplepair([rng], a)
117
123
118
124
Draw a pair of distinct elements from the array `a` without replacement.
119
125
120
126
Optionally specify a random number generator `rng` as the first argument
121
- (defaults to `Random.GLOBAL_RNG`).
127
+ (defaults to `Random.$( VERSION < v " 1.3 " ? " GLOBAL_RNG" : " default_rng() " ) `).
122
128
"""
123
129
function samplepair (rng:: AbstractRNG , a:: AbstractArray )
124
130
i1, i2 = samplepair (rng, length (a))
125
131
return a[i1], a[i2]
126
132
end
127
- samplepair (a:: AbstractArray ) = samplepair (Random . GLOBAL_RNG , a)
133
+ samplepair (a:: AbstractArray ) = samplepair (default_rng () , a)
128
134
129
135
# ## Algorithm for sampling without replacement
130
136
@@ -173,7 +179,7 @@ function knuths_sample!(rng::AbstractRNG, a::AbstractArray, x::AbstractArray;
173
179
return x
174
180
end
175
181
knuths_sample! (a:: AbstractArray , x:: AbstractArray ; initshuffle:: Bool = true ) =
176
- knuths_sample! (Random . GLOBAL_RNG , a, x; initshuffle= initshuffle)
182
+ knuths_sample! (default_rng () , a, x; initshuffle= initshuffle)
177
183
178
184
"""
179
185
fisher_yates_sample!([rng], a::AbstractArray, x::AbstractArray)
@@ -223,7 +229,7 @@ function fisher_yates_sample!(rng::AbstractRNG, a::AbstractArray, x::AbstractArr
223
229
return x
224
230
end
225
231
fisher_yates_sample! (a:: AbstractArray , x:: AbstractArray ) =
226
- fisher_yates_sample! (Random . GLOBAL_RNG , a, x)
232
+ fisher_yates_sample! (default_rng () , a, x)
227
233
228
234
"""
229
235
self_avoid_sample!([rng], a::AbstractArray, x::AbstractArray)
@@ -269,7 +275,7 @@ function self_avoid_sample!(rng::AbstractRNG, a::AbstractArray, x::AbstractArray
269
275
return x
270
276
end
271
277
self_avoid_sample! (a:: AbstractArray , x:: AbstractArray ) =
272
- self_avoid_sample! (Random . GLOBAL_RNG , a, x)
278
+ self_avoid_sample! (default_rng () , a, x)
273
279
274
280
"""
275
281
seqsample_a!([rng], a::AbstractArray, x::AbstractArray)
@@ -311,7 +317,7 @@ function seqsample_a!(rng::AbstractRNG, a::AbstractArray, x::AbstractArray)
311
317
end
312
318
return x
313
319
end
314
- seqsample_a! (a:: AbstractArray , x:: AbstractArray ) = seqsample_a! (Random . GLOBAL_RNG , a, x)
320
+ seqsample_a! (a:: AbstractArray , x:: AbstractArray ) = seqsample_a! (default_rng () , a, x)
315
321
316
322
"""
317
323
seqsample_c!([rng], a::AbstractArray, x::AbstractArray)
@@ -357,7 +363,7 @@ function seqsample_c!(rng::AbstractRNG, a::AbstractArray, x::AbstractArray)
357
363
end
358
364
return x
359
365
end
360
- seqsample_c! (a:: AbstractArray , x:: AbstractArray ) = seqsample_c! (Random . GLOBAL_RNG , a, x)
366
+ seqsample_c! (a:: AbstractArray , x:: AbstractArray ) = seqsample_c! (default_rng () , a, x)
361
367
362
368
"""
363
369
seqsample_d!([rng], a::AbstractArray, x::AbstractArray)
@@ -449,7 +455,7 @@ function seqsample_d!(rng::AbstractRNG, a::AbstractArray, x::AbstractArray)
449
455
end
450
456
end
451
457
452
- seqsample_d! (a:: AbstractArray , x:: AbstractArray ) = seqsample_d! (Random . GLOBAL_RNG , a, x)
458
+ seqsample_d! (a:: AbstractArray , x:: AbstractArray ) = seqsample_d! (default_rng () , a, x)
453
459
454
460
455
461
# ## Interface functions (poly-algorithms)
@@ -460,10 +466,10 @@ Select a single random element of `a`. Sampling probabilities are proportional t
460
466
the weights given in `wv`, if provided.
461
467
462
468
Optionally specify a random number generator `rng` as the first argument
463
- (defaults to `Random.GLOBAL_RNG`).
469
+ (defaults to `Random.$( VERSION < v " 1.3 " ? " GLOBAL_RNG" : " default_rng() " ) `).
464
470
"""
465
471
sample (rng:: AbstractRNG , a:: AbstractArray ) = a[rand (rng, 1 : length (a))]
466
- sample (a:: AbstractArray ) = sample (Random . GLOBAL_RNG , a)
472
+ sample (a:: AbstractArray ) = sample (default_rng () , a)
467
473
468
474
469
475
"""
@@ -478,7 +484,7 @@ an ordered sample (also called a sequential sample, i.e. a sample where
478
484
items appear in the same order as in `a`) should be taken.
479
485
480
486
Optionally specify a random number generator `rng` as the first argument
481
- (defaults to `Random.GLOBAL_RNG`).
487
+ (defaults to `Random.$( VERSION < v " 1.3 " ? " GLOBAL_RNG" : " default_rng() " ) `).
482
488
483
489
Output array `a` must not be the same object as `x` or `wv`
484
490
nor share memory with them, or the result may be incorrect.
@@ -522,7 +528,7 @@ function sample!(rng::AbstractRNG, a::AbstractArray, x::AbstractArray;
522
528
return x
523
529
end
524
530
sample! (a:: AbstractArray , x:: AbstractArray ; replace:: Bool = true , ordered:: Bool = false ) =
525
- sample! (Random . GLOBAL_RNG , a, x; replace= replace, ordered= ordered)
531
+ sample! (default_rng () , a, x; replace= replace, ordered= ordered)
526
532
527
533
528
534
"""
@@ -536,14 +542,14 @@ an ordered sample (also called a sequential sample, i.e. a sample where
536
542
items appear in the same order as in `a`) should be taken.
537
543
538
544
Optionally specify a random number generator `rng` as the first argument
539
- (defaults to `Random.GLOBAL_RNG`).
545
+ (defaults to `Random.$( VERSION < v " 1.3 " ? " GLOBAL_RNG" : " default_rng() " ) `).
540
546
"""
541
547
function sample (rng:: AbstractRNG , a:: AbstractArray{T} , n:: Integer ;
542
548
replace:: Bool = true , ordered:: Bool = false ) where T
543
549
sample! (rng, a, Vector {T} (undef, n); replace= replace, ordered= ordered)
544
550
end
545
551
sample (a:: AbstractArray , n:: Integer ; replace:: Bool = true , ordered:: Bool = false ) =
546
- sample (Random . GLOBAL_RNG , a, n; replace= replace, ordered= ordered)
552
+ sample (default_rng () , a, n; replace= replace, ordered= ordered)
547
553
548
554
549
555
"""
@@ -557,14 +563,14 @@ an ordered sample (also called a sequential sample, i.e. a sample where
557
563
items appear in the same order as in `a`) should be taken.
558
564
559
565
Optionally specify a random number generator `rng` as the first argument
560
- (defaults to `Random.GLOBAL_RNG`).
566
+ (defaults to `Random.$( VERSION < v " 1.3 " ? " GLOBAL_RNG" : " default_rng() " ) `).
561
567
"""
562
568
function sample (rng:: AbstractRNG , a:: AbstractArray{T} , dims:: Dims ;
563
569
replace:: Bool = true , ordered:: Bool = false ) where T
564
570
sample! (rng, a, Array {T} (undef, dims); replace= replace, ordered= ordered)
565
571
end
566
572
sample (a:: AbstractArray , dims:: Dims ; replace:: Bool = true , ordered:: Bool = false ) =
567
- sample (Random . GLOBAL_RNG , a, dims; replace= replace, ordered= ordered)
573
+ sample (default_rng () , a, dims; replace= replace, ordered= ordered)
568
574
569
575
# ###############################################################
570
576
#
@@ -579,7 +585,7 @@ Select a single random integer in `1:length(wv)` with probabilities
579
585
proportional to the weights given in `wv`.
580
586
581
587
Optionally specify a random number generator `rng` as the first argument
582
- (defaults to `Random.GLOBAL_RNG`).
588
+ (defaults to `Random.$( VERSION < v " 1.3 " ? " GLOBAL_RNG" : " default_rng() " ) `).
583
589
"""
584
590
function sample (rng:: AbstractRNG , wv:: AbstractWeights )
585
591
1 == firstindex (wv) ||
@@ -594,10 +600,10 @@ function sample(rng::AbstractRNG, wv::AbstractWeights)
594
600
end
595
601
return i
596
602
end
597
- sample (wv:: AbstractWeights ) = sample (Random . GLOBAL_RNG , wv)
603
+ sample (wv:: AbstractWeights ) = sample (default_rng () , wv)
598
604
599
605
sample (rng:: AbstractRNG , a:: AbstractArray , wv:: AbstractWeights ) = a[sample (rng, wv)]
600
- sample (a:: AbstractArray , wv:: AbstractWeights ) = sample (Random . GLOBAL_RNG , a, wv)
606
+ sample (a:: AbstractArray , wv:: AbstractWeights ) = sample (default_rng () , a, wv)
601
607
602
608
"""
603
609
direct_sample!([rng], a::AbstractArray, wv::AbstractWeights, x::AbstractArray)
@@ -627,7 +633,7 @@ function direct_sample!(rng::AbstractRNG, a::AbstractArray,
627
633
return x
628
634
end
629
635
direct_sample! (a:: AbstractArray , wv:: AbstractWeights , x:: AbstractArray ) =
630
- direct_sample! (Random . GLOBAL_RNG , a, wv, x)
636
+ direct_sample! (default_rng () , a, wv, x)
631
637
632
638
function make_alias_table! (w:: AbstractVector , wsum,
633
639
a:: AbstractVector{Float64} ,
@@ -725,7 +731,7 @@ function alias_sample!(rng::AbstractRNG, a::AbstractArray, wv::AbstractWeights,
725
731
return x
726
732
end
727
733
alias_sample! (a:: AbstractArray , wv:: AbstractWeights , x:: AbstractArray ) =
728
- alias_sample! (Random . GLOBAL_RNG , a, wv, x)
734
+ alias_sample! (default_rng () , a, wv, x)
729
735
730
736
"""
731
737
naive_wsample_norep!([rng], a::AbstractArray, wv::AbstractWeights, x::AbstractArray)
@@ -769,7 +775,7 @@ function naive_wsample_norep!(rng::AbstractRNG, a::AbstractArray,
769
775
return x
770
776
end
771
777
naive_wsample_norep! (a:: AbstractArray , wv:: AbstractWeights , x:: AbstractArray ) =
772
- naive_wsample_norep! (Random . GLOBAL_RNG , a, wv, x)
778
+ naive_wsample_norep! (default_rng () , a, wv, x)
773
779
774
780
# Weighted sampling without replacement
775
781
# Instead of keys u^(1/w) where u = random(0,1) keys w/v where v = randexp(1) are used.
@@ -810,7 +816,7 @@ function efraimidis_a_wsample_norep!(rng::AbstractRNG, a::AbstractArray,
810
816
return x
811
817
end
812
818
efraimidis_a_wsample_norep! (a:: AbstractArray , wv:: AbstractWeights , x:: AbstractArray ) =
813
- efraimidis_a_wsample_norep! (Random . GLOBAL_RNG , a, wv, x)
819
+ efraimidis_a_wsample_norep! (default_rng () , a, wv, x)
814
820
815
821
# Weighted sampling without replacement
816
822
# Instead of keys u^(1/w) where u = random(0,1) keys w/v where v = randexp(1) are used.
@@ -882,7 +888,7 @@ function efraimidis_ares_wsample_norep!(rng::AbstractRNG, a::AbstractArray,
882
888
return x
883
889
end
884
890
efraimidis_ares_wsample_norep! (a:: AbstractArray , wv:: AbstractWeights , x:: AbstractArray ) =
885
- efraimidis_ares_wsample_norep! (Random . GLOBAL_RNG , a, wv, x)
891
+ efraimidis_ares_wsample_norep! (default_rng () , a, wv, x)
886
892
887
893
# Weighted sampling without replacement
888
894
# Instead of keys u^(1/w) where u = random(0,1) keys w/v where v = randexp(1) are used.
@@ -964,7 +970,7 @@ function efraimidis_aexpj_wsample_norep!(rng::AbstractRNG, a::AbstractArray,
964
970
end
965
971
efraimidis_aexpj_wsample_norep! (a:: AbstractArray , wv:: AbstractWeights , x:: AbstractArray ;
966
972
ordered:: Bool = false ) =
967
- efraimidis_aexpj_wsample_norep! (Random . GLOBAL_RNG , a, wv, x; ordered= ordered)
973
+ efraimidis_aexpj_wsample_norep! (default_rng () , a, wv, x; ordered= ordered)
968
974
969
975
function sample! (rng:: AbstractRNG , a:: AbstractArray , wv:: AbstractWeights , x:: AbstractArray ;
970
976
replace:: Bool = true , ordered:: Bool = false )
@@ -998,21 +1004,21 @@ function sample!(rng::AbstractRNG, a::AbstractArray, wv::AbstractWeights, x::Abs
998
1004
end
999
1005
sample! (a:: AbstractArray , wv:: AbstractWeights , x:: AbstractArray ;
1000
1006
replace:: Bool = true , ordered:: Bool = false ) =
1001
- sample! (Random . GLOBAL_RNG , a, wv, x; replace= replace, ordered= ordered)
1007
+ sample! (default_rng () , a, wv, x; replace= replace, ordered= ordered)
1002
1008
1003
1009
sample (rng:: AbstractRNG , a:: AbstractArray{T} , wv:: AbstractWeights , n:: Integer ;
1004
1010
replace:: Bool = true , ordered:: Bool = false ) where {T} =
1005
1011
sample! (rng, a, wv, Vector {T} (undef, n); replace= replace, ordered= ordered)
1006
1012
sample (a:: AbstractArray , wv:: AbstractWeights , n:: Integer ;
1007
1013
replace:: Bool = true , ordered:: Bool = false ) =
1008
- sample (Random . GLOBAL_RNG , a, wv, n; replace= replace, ordered= ordered)
1014
+ sample (default_rng () , a, wv, n; replace= replace, ordered= ordered)
1009
1015
1010
1016
sample (rng:: AbstractRNG , a:: AbstractArray{T} , wv:: AbstractWeights , dims:: Dims ;
1011
1017
replace:: Bool = true , ordered:: Bool = false ) where {T} =
1012
1018
sample! (rng, a, wv, Array {T} (undef, dims); replace= replace, ordered= ordered)
1013
1019
sample (a:: AbstractArray , wv:: AbstractWeights , dims:: Dims ;
1014
1020
replace:: Bool = true , ordered:: Bool = false ) =
1015
- sample (Random . GLOBAL_RNG , a, wv, dims; replace= replace, ordered= ordered)
1021
+ sample (default_rng () , a, wv, dims; replace= replace, ordered= ordered)
1016
1022
1017
1023
# wsample interface
1018
1024
@@ -1026,14 +1032,14 @@ an ordered sample (also called a sequential sample, i.e. a sample where
1026
1032
items appear in the same order as in `a`) should be taken.
1027
1033
1028
1034
Optionally specify a random number generator `rng` as the first argument
1029
- (defaults to `Random.GLOBAL_RNG`).
1035
+ (defaults to `Random.$( VERSION < v " 1.3 " ? " GLOBAL_RNG" : " default_rng() " ) `).
1030
1036
"""
1031
1037
wsample! (rng:: AbstractRNG , a:: AbstractArray , w:: AbstractVector{<:Real} , x:: AbstractArray ;
1032
1038
replace:: Bool = true , ordered:: Bool = false ) =
1033
1039
sample! (rng, a, weights (w), x; replace= replace, ordered= ordered)
1034
1040
wsample! (a:: AbstractArray , w:: AbstractVector{<:Real} , x:: AbstractArray ;
1035
1041
replace:: Bool = true , ordered:: Bool = false ) =
1036
- sample! (Random . GLOBAL_RNG , a, weights (w), x; replace= replace, ordered= ordered)
1042
+ sample! (default_rng () , a, weights (w), x; replace= replace, ordered= ordered)
1037
1043
1038
1044
"""
1039
1045
wsample([rng], [a], w)
@@ -1042,12 +1048,12 @@ Select a weighted random sample of size 1 from `a` with probabilities proportion
1042
1048
to the weights given in `w`. If `a` is not present, select a random weight from `w`.
1043
1049
1044
1050
Optionally specify a random number generator `rng` as the first argument
1045
- (defaults to `Random.GLOBAL_RNG`).
1051
+ (defaults to `Random.$( VERSION < v " 1.3 " ? " GLOBAL_RNG" : " default_rng() " ) `).
1046
1052
"""
1047
1053
wsample (rng:: AbstractRNG , w:: AbstractVector{<:Real} ) = sample (rng, weights (w))
1048
- wsample (w:: AbstractVector{<:Real} ) = wsample (Random . GLOBAL_RNG , w)
1054
+ wsample (w:: AbstractVector{<:Real} ) = wsample (default_rng () , w)
1049
1055
wsample (rng:: AbstractRNG , a:: AbstractArray , w:: AbstractVector{<:Real} ) = sample (rng, a, weights (w))
1050
- wsample (a:: AbstractArray , w:: AbstractVector{<:Real} ) = wsample (Random . GLOBAL_RNG , a, w)
1056
+ wsample (a:: AbstractArray , w:: AbstractVector{<:Real} ) = wsample (default_rng () , a, w)
1051
1057
1052
1058
1053
1059
"""
@@ -1061,14 +1067,14 @@ an ordered sample (also called a sequential sample, i.e. a sample where
1061
1067
items appear in the same order as in `a`) should be taken.
1062
1068
1063
1069
Optionally specify a random number generator `rng` as the first argument
1064
- (defaults to `Random.GLOBAL_RNG`).
1070
+ (defaults to `Random.$( VERSION < v " 1.3 " ? " GLOBAL_RNG" : " default_rng() " ) `).
1065
1071
"""
1066
1072
wsample (rng:: AbstractRNG , a:: AbstractArray{T} , w:: AbstractVector{<:Real} , n:: Integer ;
1067
1073
replace:: Bool = true , ordered:: Bool = false ) where {T} =
1068
1074
wsample! (rng, a, w, Vector {T} (undef, n); replace= replace, ordered= ordered)
1069
1075
wsample (a:: AbstractArray , w:: AbstractVector{<:Real} , n:: Integer ;
1070
1076
replace:: Bool = true , ordered:: Bool = false ) =
1071
- wsample (Random . GLOBAL_RNG , a, w, n; replace= replace, ordered= ordered)
1077
+ wsample (default_rng () , a, w, n; replace= replace, ordered= ordered)
1072
1078
1073
1079
"""
1074
1080
wsample([rng], [a], w, dims::Dims; replace=true, ordered=false)
@@ -1078,11 +1084,11 @@ weights given in `w` if `a` is present, otherwise select a random sample of size
1078
1084
`n` of the weights given in `w`. The dimensions of the output are given by `dims`.
1079
1085
1080
1086
Optionally specify a random number generator `rng` as the first argument
1081
- (defaults to `Random.GLOBAL_RNG`).
1087
+ (defaults to `Random.$( VERSION < v " 1.3 " ? " GLOBAL_RNG" : " default_rng() " ) `).
1082
1088
"""
1083
1089
wsample (rng:: AbstractRNG , a:: AbstractArray{T} , w:: AbstractVector{<:Real} , dims:: Dims ;
1084
1090
replace:: Bool = true , ordered:: Bool = false ) where {T} =
1085
1091
wsample! (rng, a, w, Array {T} (undef, dims); replace= replace, ordered= ordered)
1086
1092
wsample (a:: AbstractArray , w:: AbstractVector{<:Real} , dims:: Dims ;
1087
1093
replace:: Bool = true , ordered:: Bool = false ) =
1088
- wsample (Random . GLOBAL_RNG , a, w, dims; replace= replace, ordered= ordered)
1094
+ wsample (default_rng () , a, w, dims; replace= replace, ordered= ordered)
0 commit comments