@@ -2020,15 +2020,18 @@ module Array =
2020
2020
let inputLength = source.Length
2021
2021
2022
2022
if inputLength = 0 then
2023
- invalidArg " source" LanguagePrimitives.ErrorStrings.InputArrayEmptyString
2024
-
2025
- let result = Microsoft.FSharp.Primitives.Basics.Array.zeroCreateUnchecked count
2023
+ if count = 0 then
2024
+ [||]
2025
+ else
2026
+ invalidArg " source" LanguagePrimitives.ErrorStrings.InputSequenceEmptyString
2027
+ else
2028
+ let result = Microsoft.FSharp.Primitives.Basics.Array.zeroCreateUnchecked count
2026
2029
2027
- for i = 0 to count - 1 do
2028
- let j = random.Next( 0 , inputLength)
2029
- result[ i] <- source[ j]
2030
+ for i = 0 to count - 1 do
2031
+ let j = random.Next( 0 , inputLength)
2032
+ result[ i] <- source[ j]
2030
2033
2031
- result
2034
+ result
2032
2035
2033
2036
[<CompiledName( " RandomChoicesBy" ) >]
2034
2037
let randomChoicesBy ( randomizer : unit -> float ) ( count : int ) ( source : 'T array ) : 'T array =
@@ -2040,15 +2043,18 @@ module Array =
2040
2043
let inputLength = source.Length
2041
2044
2042
2045
if inputLength = 0 then
2043
- invalidArg " source" LanguagePrimitives.ErrorStrings.InputArrayEmptyString
2044
-
2045
- let result = Microsoft.FSharp.Primitives.Basics.Array.zeroCreateUnchecked count
2046
+ if count = 0 then
2047
+ [||]
2048
+ else
2049
+ invalidArg " source" LanguagePrimitives.ErrorStrings.InputSequenceEmptyString
2050
+ else
2051
+ let result = Microsoft.FSharp.Primitives.Basics.Array.zeroCreateUnchecked count
2046
2052
2047
- for i = 0 to count - 1 do
2048
- let j = Microsoft.FSharp.Primitives.Basics.Random.next randomizer 0 inputLength
2049
- result[ i] <- source[ j]
2053
+ for i = 0 to count - 1 do
2054
+ let j = Microsoft.FSharp.Primitives.Basics.Random.next randomizer 0 inputLength
2055
+ result[ i] <- source[ j]
2050
2056
2051
- result
2057
+ result
2052
2058
2053
2059
[<CompiledName( " RandomChoices" ) >]
2054
2060
let randomChoices ( count : int ) ( source : 'T array ) : 'T array =
@@ -2065,35 +2071,38 @@ module Array =
2065
2071
let inputLength = source.Length
2066
2072
2067
2073
if inputLength = 0 then
2068
- invalidArg " source" LanguagePrimitives.ErrorStrings.InputArrayEmptyString
2069
-
2070
- if count > inputLength then
2071
- invalidArg " count" ( SR.GetString( SR.notEnoughElements))
2074
+ if count = 0 then
2075
+ [||]
2076
+ else
2077
+ invalidArg " source" LanguagePrimitives.ErrorStrings.InputSequenceEmptyString
2078
+ else
2079
+ if count > inputLength then
2080
+ invalidArg " count" ( SR.GetString( SR.notEnoughElements))
2072
2081
2073
- let result = Microsoft.FSharp.Primitives.Basics.Array.zeroCreateUnchecked count
2082
+ let result = Microsoft.FSharp.Primitives.Basics.Array.zeroCreateUnchecked count
2074
2083
2075
- let setSize =
2076
- Microsoft.FSharp.Primitives.Basics.Random.getMaxSetSizeForSampling count
2084
+ let setSize =
2085
+ Microsoft.FSharp.Primitives.Basics.Random.getMaxSetSizeForSampling count
2077
2086
2078
- if inputLength <= setSize then
2079
- let pool = copy source
2087
+ if inputLength <= setSize then
2088
+ let pool = copy source
2080
2089
2081
- for i = 0 to count - 1 do
2082
- let j = random.Next( 0 , inputLength - i)
2083
- result[ i] <- pool[ j]
2084
- pool[ j] <- pool[ inputLength - i - 1 ]
2085
- else
2086
- let selected = HashSet()
2090
+ for i = 0 to count - 1 do
2091
+ let j = random.Next( 0 , inputLength - i)
2092
+ result[ i] <- pool[ j]
2093
+ pool[ j] <- pool[ inputLength - i - 1 ]
2094
+ else
2095
+ let selected = HashSet()
2087
2096
2088
- for i = 0 to count - 1 do
2089
- let mutable j = random.Next( 0 , inputLength)
2097
+ for i = 0 to count - 1 do
2098
+ let mutable j = random.Next( 0 , inputLength)
2090
2099
2091
- while not ( selected.Add j) do
2092
- j <- random.Next( 0 , inputLength)
2100
+ while not ( selected.Add j) do
2101
+ j <- random.Next( 0 , inputLength)
2093
2102
2094
- result[ i] <- source[ j]
2103
+ result[ i] <- source[ j]
2095
2104
2096
- result
2105
+ result
2097
2106
2098
2107
[<CompiledName( " RandomSampleBy" ) >]
2099
2108
let randomSampleBy ( randomizer : unit -> float ) ( count : int ) ( source : 'T array ) : 'T array =
@@ -2105,39 +2114,42 @@ module Array =
2105
2114
let inputLength = source.Length
2106
2115
2107
2116
if inputLength = 0 then
2108
- invalidArg " source" LanguagePrimitives.ErrorStrings.InputArrayEmptyString
2109
-
2110
- if count > inputLength then
2111
- invalidArg " count" ( SR.GetString( SR.notEnoughElements))
2117
+ if count = 0 then
2118
+ [||]
2119
+ else
2120
+ invalidArg " source" LanguagePrimitives.ErrorStrings.InputSequenceEmptyString
2121
+ else
2122
+ if count > inputLength then
2123
+ invalidArg " count" ( SR.GetString( SR.notEnoughElements))
2112
2124
2113
- let result = Microsoft.FSharp.Primitives.Basics.Array.zeroCreateUnchecked count
2125
+ let result = Microsoft.FSharp.Primitives.Basics.Array.zeroCreateUnchecked count
2114
2126
2115
- // algorithm taken from https://github.com/python/cpython/blob/69b3e8ea569faabccd74036e3d0e5ec7c0c62a20/Lib/random.py#L363-L456
2116
- let setSize =
2117
- Microsoft.FSharp.Primitives.Basics.Random.getMaxSetSizeForSampling count
2127
+ // algorithm taken from https://github.com/python/cpython/blob/69b3e8ea569faabccd74036e3d0e5ec7c0c62a20/Lib/random.py#L363-L456
2128
+ let setSize =
2129
+ Microsoft.FSharp.Primitives.Basics.Random.getMaxSetSizeForSampling count
2118
2130
2119
- if inputLength <= setSize then
2120
- let pool = copy source
2131
+ if inputLength <= setSize then
2132
+ let pool = copy source
2121
2133
2122
- for i = 0 to count - 1 do
2123
- let j =
2124
- Microsoft.FSharp.Primitives.Basics.Random.next randomizer 0 ( inputLength - i)
2134
+ for i = 0 to count - 1 do
2135
+ let j =
2136
+ Microsoft.FSharp.Primitives.Basics.Random.next randomizer 0 ( inputLength - i)
2125
2137
2126
- result[ i] <- pool[ j]
2127
- pool[ j] <- pool[ inputLength - i - 1 ]
2128
- else
2129
- let selected = HashSet()
2138
+ result[ i] <- pool[ j]
2139
+ pool[ j] <- pool[ inputLength - i - 1 ]
2140
+ else
2141
+ let selected = HashSet()
2130
2142
2131
- for i = 0 to count - 1 do
2132
- let mutable j =
2133
- Microsoft.FSharp.Primitives.Basics.Random.next randomizer 0 inputLength
2143
+ for i = 0 to count - 1 do
2144
+ let mutable j =
2145
+ Microsoft.FSharp.Primitives.Basics.Random.next randomizer 0 inputLength
2134
2146
2135
- while not ( selected.Add j) do
2136
- j <- Microsoft.FSharp.Primitives.Basics.Random.next randomizer 0 inputLength
2147
+ while not ( selected.Add j) do
2148
+ j <- Microsoft.FSharp.Primitives.Basics.Random.next randomizer 0 inputLength
2137
2149
2138
- result[ i] <- source[ j]
2150
+ result[ i] <- source[ j]
2139
2151
2140
- result
2152
+ result
2141
2153
2142
2154
[<CompiledName( " RandomSample" ) >]
2143
2155
let randomSample ( count : int ) ( source : 'T array ) : 'T array =
0 commit comments