Perform an unbiased random shuffling of the input array, known as the Knuth or Fisher-Yates shuffle, and generate and return the new reshuffled array.
- Parameters
-
[in] | array | : The input contiguous array of shape (:) of either
-
type
character of kind any supported by the processor (e.g., SK, SKA, SKD , or SKU),
-
type
logical of kind any supported by the processor (e.g., LK),
-
type
integer of kind any supported by the processor (e.g., IK, IK8, IK16, IK32, or IK64),
-
type
complex of kind any supported by the processor (e.g., CK, CK32, CK64, or CK128),
-
type
real of kind any supported by the processor (e.g., RK, RK32, RK64, or RK128),
-
type css_pdt,
-
type css_type,
or,
-
a scalar assumed-length
character of kind any supported by the processor (e.g., SK, SKA, SKD , or SKU),
whose elements will be shuffled uniformly-randomly in the output arrayShuffled on return.
|
[in] | count | : The input positive scalar integer of default kind IK, containing the number of elements of the unique uniformly-random draws (shuffled elements) from the input array .
The specified count must not be larger than the length of the input sequence array .
(optional, default = len(array) for scalar character input array , otherwise size(array) .) |
- Returns
arrayShuffled
: The output allocatable
object of the same type, kind, rank, and shape as the input array
of size (1:count)
whose elements are set from the randomly-shuffled elements of array
.
Possible calling interfaces ⛓
Perform an unbiased random shuffling of the input array, known as the Knuth or Fisher-Yates shuffle,...
This module contains procedures and generic interfaces for shuffling arrays of various types.
- Warning
- The condition
0 <= count
must hold for the corresponding input arguments.
The condition count <= lenArray
must hold for the corresponding input arguments where lenArray
represents the length of the input sequence.
These conditions are verified only if the library is built with the preprocessor macro CHECK_ENABLED=1
.
- See also
- getChoice
setChoice
setShuffled
getRemapped
setRemapped
getReversed
setReversed
Example usage ⛓
15 integer(IK) :: count, itry, ntry
= 10
16 type(display_type) :: disp
20 character(:),
allocatable :: array
24 call disp%show(
"count = getUnifRand(4, 10)")
26 call disp%show(
"array = getUnifRand(repeat('A', count), repeat('Z', count)) ! generate random array for illustration.")
27 array
= getUnifRand(
repeat(
'A', count),
repeat(
'Z', count))
29 call disp%show( array, deliml
= SK_
"""" )
30 call disp%show(
"array = getShuffled(array)")
33 call disp%show( array, deliml
= SK_
"""" )
34 call disp%show(
"count = getUnifRand(0, len(array))")
38 call disp%show(
"array = getShuffled(array, count) ! draw randomly only `count` elements without replacement.")
41 call disp%show( array, deliml
= SK_
"""" )
47 character(
2),
allocatable :: array(:)
51 call disp%show(
"count = getUnifRand(4, 10)")
53 call disp%show(
"array = getUnifRand('AA', 'ZZ', count) ! generate random array for illustration.")
56 call disp%show( array, deliml
= SK_
"""" )
57 call disp%show(
"array = getShuffled(array)")
60 call disp%show( array, deliml
= SK_
"""" )
61 call disp%show(
"count = getUnifRand(0, size(array))")
65 call disp%show(
"array = getShuffled(array, count) ! draw randomly only `count` elements without replacement.")
68 call disp%show( array, deliml
= SK_
"""" )
74 integer,
allocatable :: array(:)
78 call disp%show(
"count = getUnifRand(4, 10)")
80 call disp%show(
"array = getUnifRand(0, 9, count) ! generate random array for illustration.")
84 call disp%show(
"array = getShuffled(array)")
88 call disp%show(
"count = getUnifRand(0, size(array))")
92 call disp%show(
"array = getShuffled(array, count) ! draw randomly only `count` elements without replacement.")
101 logical,
allocatable :: array(:)
105 call disp%show(
"count = getUnifRand(4, 10)")
107 call disp%show(
"array = getUnifRand(.false., .true., count) ! generate random array for illustration.")
111 call disp%show(
"array = getShuffled(array)")
115 call disp%show(
"count = getUnifRand(0, size(array))")
119 call disp%show(
"array = getShuffled(array, count) ! draw randomly only `count` elements without replacement.")
128 complex,
allocatable :: array(:)
132 call disp%show(
"count = getUnifRand(4, 10)")
134 call disp%show(
"array = getUnifRand((0., 0.), (1., 1.), count) ! generate random array for illustration.")
138 call disp%show(
"array = getShuffled(array)")
142 call disp%show(
"count = getUnifRand(0, size(array))")
146 call disp%show(
"array = getShuffled(array, count) ! draw randomly only `count` elements without replacement.")
155 real,
allocatable :: array(:)
159 call disp%show(
"count = getUnifRand(4, 10)")
161 call disp%show(
"array = getUnifRand(0., 1., count) ! generate random array for illustration.")
165 call disp%show(
"array = getShuffled(array)")
169 call disp%show(
"count = getUnifRand(0, size(array))")
173 call disp%show(
"array = getShuffled(array, count) ! draw randomly only `count` elements without replacement.")
Generate minimally-spaced character, integer, real sequences or sequences at fixed intervals of size ...
Generate and return a scalar or a contiguous array of rank 1 of length s1 of randomly uniformly distr...
This is a generic method of the derived type display_type with pass attribute.
This is a generic method of the derived type display_type with pass attribute.
This module contains procedures and generic interfaces for generating ranges of discrete character,...
This module contains classes and procedures for computing various statistical quantities related to t...
This module contains classes and procedures for input/output (IO) or generic display operations on st...
type(display_type) disp
This is a scalar module variable an object of type display_type for general display.
This module defines the relevant Fortran kind type-parameters frequently used in the ParaMonte librar...
integer, parameter RK
The default real kind in the ParaMonte library: real64 in Fortran, c_double in C-Fortran Interoperati...
integer, parameter LK
The default logical kind in the ParaMonte library: kind(.true.) in Fortran, kind(....
integer, parameter CK
The default complex kind in the ParaMonte library: real64 in Fortran, c_double_complex in C-Fortran I...
integer, parameter IK
The default integer kind in the ParaMonte library: int32 in Fortran, c_int32_t in C-Fortran Interoper...
integer, parameter SK
The default character kind in the ParaMonte library: kind("a") in Fortran, c_char in C-Fortran Intero...
Generate and return an object of type display_type.
Example Unix compile command via Intel ifort
compiler ⛓
3ifort -fpp -standard-semantics -O3 -Wl,-rpath,../../../lib -I../../../inc main.F90 ../../../lib/libparamonte* -o main.exe
Example Windows Batch compile command via Intel ifort
compiler ⛓
2set PATH=..\..\..\lib;%PATH%
3ifort /fpp /standard-semantics /O3 /I:..\..\..\include main.F90 ..\..\..\lib\libparamonte*.lib /exe:main.exe
Example Unix / MinGW compile command via GNU gfortran
compiler ⛓
3gfortran -cpp -ffree-line-length-none -O3 -Wl,-rpath,../../../lib -I../../../inc main.F90 ../../../lib/libparamonte* -o main.exe
Example output ⛓
3array
= getUnifRand(
repeat(
'A', count),
repeat(
'Z', count))
17array
= getUnifRand(
repeat(
'A', count),
repeat(
'Z', count))
31array
= getUnifRand(
repeat(
'A', count),
repeat(
'Z', count))
45array
= getUnifRand(
repeat(
'A', count),
repeat(
'Z', count))
59array
= getUnifRand(
repeat(
'A', count),
repeat(
'Z', count))
73array
= getUnifRand(
repeat(
'A', count),
repeat(
'Z', count))
87array
= getUnifRand(
repeat(
'A', count),
repeat(
'Z', count))
101array
= getUnifRand(
repeat(
'A', count),
repeat(
'Z', count))
115array
= getUnifRand(
repeat(
'A', count),
repeat(
'Z', count))
129array
= getUnifRand(
repeat(
'A', count),
repeat(
'Z', count))
145"NE",
"MP",
"BE",
"JP",
"LY",
"RU",
"EL",
"LX",
"SX"
148"JP",
"BE",
"MP",
"NE",
"EL",
"LX",
"LY",
"RU",
"SX"
154"RU",
"LY",
"MP",
"EL",
"SX"
159"KE",
"IP",
"ME",
"OX",
"DD",
"YF"
162"ME",
"OX",
"IP",
"DD",
"KE",
"YF"
173"UY",
"JO",
"WZ",
"WF",
"NH"
176"UY",
"WF",
"WZ",
"JO",
"NH"
187"LF",
"JC",
"QU",
"MC",
"KY",
"BG",
"EN"
190"MC",
"QU",
"JC",
"KY",
"BG",
"EN",
"LF"
196"QU",
"KY",
"BG",
"LF"
201"GB",
"UP",
"HW",
"HS",
"PQ",
"KK",
"KD",
"GV",
"CL"
204"HW",
"HS",
"GV",
"CL",
"PQ",
"GB",
"KK",
"KD",
"UP"
210"GV",
"UP",
"CL",
"KK",
"GB",
"HS"
215"DV",
"FM",
"YT",
"NK",
"YC",
"LD"
218"DV",
"YT",
"YC",
"LD",
"FM",
"NK"
229"VV",
"BC",
"MP",
"MK",
"EP",
"RR",
"XP",
"PT"
232"BC",
"RR",
"PT",
"MK",
"MP",
"EP",
"VV",
"XP"
243"BW",
"BH",
"YG",
"RE",
"TF",
"BD"
246"BH",
"BD",
"RE",
"YG",
"TF",
"BW"
252"TF",
"RE",
"BH",
"BW"
257"KM",
"YM",
"PY",
"JN",
"DQ",
"AY",
"FD",
"WN",
"BD"
260"PY",
"WN",
"JN",
"YM",
"KM",
"BD",
"AY",
"DQ",
"FD"
271"WV",
"QX",
"CZ",
"BI",
"QA"
274"CZ",
"QA",
"QX",
"WV",
"BI"
280"BI",
"CZ",
"WV",
"QA",
"QX"
313+3,
+0,
+0,
+8,
+1,
+7,
+6
316+1,
+8,
+0,
+6,
+3,
+7,
+0
327+0,
+2,
+9,
+0,
+7,
+4
330+7,
+0,
+0,
+4,
+9,
+2
336+7,
+9,
+0,
+0,
+2,
+4
341+4,
+5,
+2,
+2,
+9,
+0,
+2,
+7,
+4
344+2,
+7,
+9,
+4,
+5,
+2,
+4,
+0,
+2
397+3,
+9,
+4,
+7,
+7,
+9
400+9,
+4,
+7,
+9,
+3,
+7
411+5,
+7,
+2,
+9,
+0,
+6,
+8
414+0,
+7,
+5,
+9,
+2,
+8,
+6
481F, T, T, T, F, T, F, T
484T, F, T, T, T, F, F, T
551T, T, T, T, F, T, F, F, F, T
554F, F, F, T, T, T, T, T, T, F
560F, T, F, T, T, F, T, T, F, T
565(
+0.317192912,
+0.452004731), (
+0.466819406E-1,
+0.535630465), (
+0.746624351,
+0.253590763), (
+0.100054741,
+0.588546693), (
+0.677329004,
+0.288229167)
568(
+0.466819406E-1,
+0.535630465), (
+0.677329004,
+0.288229167), (
+0.746624351,
+0.253590763), (
+0.317192912,
+0.452004731), (
+0.100054741,
+0.588546693)
574(
+0.317192912,
+0.452004731), (
+0.746624351,
+0.253590763), (
+0.100054741,
+0.588546693), (
+0.677329004,
+0.288229167)
579(
+0.627189517,
+0.626557350), (
+0.350541949,
+0.108194530), (
+0.867333353,
+0.347411096), (
+0.793159008E-1,
+0.323844016), (
+0.411503077,
+0.573862970)
582(
+0.627189517,
+0.626557350), (
+0.350541949,
+0.108194530), (
+0.793159008E-1,
+0.323844016), (
+0.411503077,
+0.573862970), (
+0.867333353,
+0.347411096)
588(
+0.627189517,
+0.626557350), (
+0.350541949,
+0.108194530), (
+0.411503077,
+0.573862970), (
+0.867333353,
+0.347411096)
593(
+0.800109386,
+0.591072857), (
+0.801631153,
+0.318058193), (
+0.590119243,
+0.991975427), (
+0.128134310,
+0.992570817), (
+0.797223270,
+0.364309847), (
+0.929321766,
+0.371339321E-1), (
+0.456440628,
+0.284176469E-1), (
+0.492525935,
+0.660563946), (
+0.157333076,
+0.718464315)
596(
+0.929321766,
+0.371339321E-1), (
+0.590119243,
+0.991975427), (
+0.456440628,
+0.284176469E-1), (
+0.801631153,
+0.318058193), (
+0.128134310,
+0.992570817), (
+0.492525935,
+0.660563946), (
+0.797223270,
+0.364309847), (
+0.157333076,
+0.718464315), (
+0.800109386,
+0.591072857)
602(
+0.157333076,
+0.718464315), (
+0.801631153,
+0.318058193), (
+0.590119243,
+0.991975427), (
+0.929321766,
+0.371339321E-1), (
+0.128134310,
+0.992570817), (
+0.800109386,
+0.591072857), (
+0.456440628,
+0.284176469E-1)
607(
+0.268506885,
+0.842541873), (
+0.292266250,
+0.547934651), (
+0.889964104E-1,
+0.612549067), (
+0.383528292,
+0.308706939), (
+0.678492367,
+0.602693021)
610(
+0.292266250,
+0.547934651), (
+0.678492367,
+0.602693021), (
+0.268506885,
+0.842541873), (
+0.889964104E-1,
+0.612549067), (
+0.383528292,
+0.308706939)
616(
+0.268506885,
+0.842541873), (
+0.292266250,
+0.547934651)
621(
+0.103776872,
+0.240086854), (
+0.970195055,
+0.658509731E-1), (
+0.921001732,
+0.495144069), (
+0.355695903,
+0.881768525), (
+0.946150005,
+0.132198870), (
+0.644226134,
+0.982109368)
624(
+0.355695903,
+0.881768525), (
+0.946150005,
+0.132198870), (
+0.970195055,
+0.658509731E-1), (
+0.644226134,
+0.982109368), (
+0.103776872,
+0.240086854), (
+0.921001732,
+0.495144069)
635(
+0.739241838,
+0.973634601), (
+0.914921701,
+0.527829349), (
+0.708583951,
+0.253438652), (
+0.187454104,
+0.181678116), (
+0.912250280,
+0.260435283), (
+0.507740319,
+0.548435032)
638(
+0.912250280,
+0.260435283), (
+0.708583951,
+0.253438652), (
+0.507740319,
+0.548435032), (
+0.914921701,
+0.527829349), (
+0.739241838,
+0.973634601), (
+0.187454104,
+0.181678116)
644(
+0.507740319,
+0.548435032), (
+0.708583951,
+0.253438652), (
+0.187454104,
+0.181678116), (
+0.914921701,
+0.527829349), (
+0.912250280,
+0.260435283)
649(
+0.502084434,
+0.210427165), (
+0.409773290,
+0.936624229), (
+0.197861969,
+0.134814382), (
+0.757906020,
+0.993931711), (
+0.723881960,
+0.726955235), (
+0.484842420,
+0.380497873), (
+0.526067853,
+0.643285692), (
+0.182982564,
+0.572760880), (
+0.778287649,
+0.137004137)
652(
+0.526067853,
+0.643285692), (
+0.409773290,
+0.936624229), (
+0.197861969,
+0.134814382), (
+0.182982564,
+0.572760880), (
+0.502084434,
+0.210427165), (
+0.757906020,
+0.993931711), (
+0.723881960,
+0.726955235), (
+0.484842420,
+0.380497873), (
+0.778287649,
+0.137004137)
658(
+0.757906020,
+0.993931711), (
+0.197861969,
+0.134814382), (
+0.484842420,
+0.380497873), (
+0.409773290,
+0.936624229), (
+0.182982564,
+0.572760880), (
+0.778287649,
+0.137004137), (
+0.526067853,
+0.643285692), (
+0.502084434,
+0.210427165)
663(
+0.886510611E-1,
+0.992991388), (
+0.789501190,
+0.280396342), (
+0.898063779E-1,
+0.982242048), (
+0.263778865,
+0.479088366), (
+0.934063435,
+0.237498283), (
+0.622903943,
+0.437605500), (
+0.854397178,
+0.220434606), (
+0.125190616E-1,
+0.277341366), (
+0.349693120,
+0.878956735), (
+0.484167159,
+0.780246854E-1)
666(
+0.898063779E-1,
+0.982242048), (
+0.349693120,
+0.878956735), (
+0.789501190,
+0.280396342), (
+0.484167159,
+0.780246854E-1), (
+0.934063435,
+0.237498283), (
+0.125190616E-1,
+0.277341366), (
+0.622903943,
+0.437605500), (
+0.854397178,
+0.220434606), (
+0.263778865,
+0.479088366), (
+0.886510611E-1,
+0.992991388)
677(
+0.236724019E-1,
+0.206309617), (
+0.784792423,
+0.701056242), (
+0.659558713,
+0.709384382), (
+0.407980680,
+0.434190035E-1), (
+0.448650718E-1,
+0.285369158E-2), (
+0.475870371E-1,
+0.148730755)
680(
+0.236724019E-1,
+0.206309617), (
+0.659558713,
+0.709384382), (
+0.407980680,
+0.434190035E-1), (
+0.448650718E-1,
+0.285369158E-2), (
+0.475870371E-1,
+0.148730755), (
+0.784792423,
+0.701056242)
686(
+0.407980680,
+0.434190035E-1)
691(
+0.839085281,
+0.123452783), (
+0.944589078,
+0.498986244E-2), (
+0.358630538,
+0.646822095), (
+0.113746524E-1,
+0.358310759), (
+0.598113775,
+0.764192820), (
+0.374714136E-1,
+0.931995034), (
+0.282228589,
+0.686004162), (
+0.464748204,
+0.542207599), (
+0.156427205,
+0.587688148), (
+0.261585593,
+0.981818080)
694(
+0.282228589,
+0.686004162), (
+0.839085281,
+0.123452783), (
+0.358630538,
+0.646822095), (
+0.374714136E-1,
+0.931995034), (
+0.113746524E-1,
+0.358310759), (
+0.261585593,
+0.981818080), (
+0.464748204,
+0.542207599), (
+0.156427205,
+0.587688148), (
+0.944589078,
+0.498986244E-2), (
+0.598113775,
+0.764192820)
705+0.103490829,
+0.655597150,
+0.685547829,
+0.353487313,
+0.402303278
708+0.655597150,
+0.103490829,
+0.402303278,
+0.353487313,
+0.685547829
714+0.353487313,
+0.685547829,
+0.402303278,
+0.655597150,
+0.103490829
719+0.387106895,
+0.118154466,
+0.875806689,
+0.977987826,
+0.548606992,
+0.773658752E-1,
+0.602793932,
+0.881369829,
+0.121393085
722+0.875806689,
+0.881369829,
+0.548606992,
+0.118154466,
+0.977987826,
+0.602793932,
+0.121393085,
+0.387106895,
+0.773658752E-1
728+0.118154466,
+0.602793932,
+0.881369829,
+0.875806689,
+0.977987826,
+0.548606992
733+0.262931287,
+0.408133626,
+0.978778064,
+0.237863123,
+0.134969592,
+0.851609111E-1,
+0.935131133
736+0.851609111E-1,
+0.134969592,
+0.408133626,
+0.978778064,
+0.237863123,
+0.262931287,
+0.935131133
742+0.935131133,
+0.408133626,
+0.134969592,
+0.237863123
747+0.655021429,
+0.996480525,
+0.936402678E-1,
+0.805007517,
+0.296100855
750+0.296100855,
+0.655021429,
+0.805007517,
+0.936402678E-1,
+0.996480525
756+0.805007517,
+0.655021429,
+0.296100855
761+0.330810905,
+0.421787620,
+0.204466879,
+0.547792435,
+0.461079061
764+0.421787620,
+0.330810905,
+0.461079061,
+0.204466879,
+0.547792435
775+0.719959497,
+0.146506131,
+0.380503774,
+0.189287484
778+0.146506131,
+0.189287484,
+0.719959497,
+0.380503774
784+0.189287484,
+0.380503774,
+0.719959497,
+0.146506131
789+0.868238509,
+0.521131754E-1,
+0.312158465,
+0.729215860,
+0.423000932,
+0.287313998,
+0.500873625,
+0.176197290E-2,
+0.209888756
792+0.868238509,
+0.729215860,
+0.500873625,
+0.209888756,
+0.287313998,
+0.521131754E-1,
+0.312158465,
+0.176197290E-2,
+0.423000932
798+0.500873625,
+0.209888756,
+0.868238509,
+0.176197290E-2,
+0.423000932,
+0.729215860,
+0.312158465,
+0.521131754E-1
803+0.299190223,
+0.187425673,
+0.104011893,
+0.820688009,
+0.163405240,
+0.657234788E-1
806+0.163405240,
+0.104011893,
+0.657234788E-1,
+0.820688009,
+0.187425673,
+0.299190223
812+0.657234788E-1,
+0.820688009,
+0.187425673,
+0.163405240,
+0.104011893
817+0.977609038,
+0.629142880,
+0.747443914,
+0.999770164,
+0.361863971,
+0.937482715E-1,
+0.915633440
820+0.629142880,
+0.999770164,
+0.747443914,
+0.361863971,
+0.937482715E-1,
+0.915633440,
+0.977609038
826+0.629142880,
+0.915633440,
+0.747443914,
+0.937482715E-1,
+0.999770164,
+0.977609038,
+0.361863971
831+0.634543538,
+0.430700958,
+0.202026963E-1,
+0.550926208
834+0.550926208,
+0.430700958,
+0.202026963E-1,
+0.634543538
- Test:
- test_pm_arrayShuffle
- Todo:
- Low Priority: This generic interface can be extended to 2D input objects.
Final Remarks ⛓
If you believe this algorithm or its documentation can be improved, we appreciate your contribution and help to edit this page's documentation and source file on GitHub.
For details on the naming abbreviations, see this page.
For details on the naming conventions, see this page.
This software is distributed under the MIT license with additional terms outlined below.
-
If you use any parts or concepts from this library to any extent, please acknowledge the usage by citing the relevant publications of the ParaMonte library.
-
If you regenerate any parts/ideas from this library in a programming environment other than those currently supported by this ParaMonte library (i.e., other than C, C++, Fortran, MATLAB, Python, R), please also ask the end users to cite this original ParaMonte library.
This software is available to the public under a highly permissive license.
Help us justify its continued development and maintenance by acknowledging its benefit to society, distributing it, and contributing to it.
- Copyright
- Computational Data Science Lab
- Author:
- Amir Shahmoradi, September 1, 2017, 12:00 AM, Institute for Computational Engineering and Sciences (ICES), The University of Texas Austin
Definition at line 145 of file pm_arrayShuffle.F90.