@@ -1936,6 +1936,211 @@ module Array =
19361936
19371937 result
19381938
1939+ [<CompiledName( " RandomShuffleWith" ) >]
1940+ let randomShuffleWith ( random : Random ) ( source : 'T array ) : 'T array =
1941+ checkNonNull " random" random
1942+ checkNonNull " source" source
1943+
1944+ let result = copy source
1945+
1946+ Microsoft.FSharp.Primitives.Basics.Random.shuffleArrayInPlaceWith random result
1947+
1948+ result
1949+
1950+ [<CompiledName( " RandomShuffleBy" ) >]
1951+ let randomShuffleBy ( randomizer : unit -> float ) ( source : 'T array ) : 'T array =
1952+ checkNonNull " source" source
1953+
1954+ let result = copy source
1955+
1956+ Microsoft.FSharp.Primitives.Basics.Random.shuffleArrayInPlaceBy randomizer result
1957+
1958+ result
1959+
1960+ [<CompiledName( " RandomShuffle" ) >]
1961+ let randomShuffle ( source : 'T array ) : 'T array =
1962+ randomShuffleWith ThreadSafeRandom.Shared source
1963+
1964+ [<CompiledName( " RandomShuffleInPlaceWith" ) >]
1965+ let randomShuffleInPlaceWith ( random : Random ) ( source : 'T array ) =
1966+ checkNonNull " random" random
1967+ checkNonNull " source" source
1968+
1969+ Microsoft.FSharp.Primitives.Basics.Random.shuffleArrayInPlaceWith random source
1970+
1971+ [<CompiledName( " RandomShuffleInPlaceBy" ) >]
1972+ let randomShuffleInPlaceBy ( randomizer : unit -> float ) ( source : 'T array ) =
1973+ checkNonNull " source" source
1974+
1975+ Microsoft.FSharp.Primitives.Basics.Random.shuffleArrayInPlaceBy randomizer source
1976+
1977+ [<CompiledName( " RandomShuffleInPlace" ) >]
1978+ let randomShuffleInPlace ( source : 'T array ) =
1979+ randomShuffleInPlaceWith ThreadSafeRandom.Shared source
1980+
1981+ [<CompiledName( " RandomChoiceWith" ) >]
1982+ let randomChoiceWith ( random : Random ) ( source : 'T array ) : 'T =
1983+ checkNonNull " random" random
1984+ checkNonNull " source" source
1985+
1986+ let inputLength = source.Length
1987+
1988+ if inputLength = 0 then
1989+ invalidArg " source" LanguagePrimitives.ErrorStrings.InputArrayEmptyString
1990+
1991+ let i = random.Next( 0 , inputLength)
1992+ source[ i]
1993+
1994+ [<CompiledName( " RandomChoiceBy" ) >]
1995+ let randomChoiceBy ( randomizer : unit -> float ) ( source : 'T array ) : 'T =
1996+ checkNonNull " source" source
1997+
1998+ let inputLength = source.Length
1999+
2000+ if inputLength = 0 then
2001+ invalidArg " source" LanguagePrimitives.ErrorStrings.InputArrayEmptyString
2002+
2003+ let i = Microsoft.FSharp.Primitives.Basics.Random.next randomizer 0 inputLength
2004+ source[ i]
2005+
2006+ [<CompiledName( " RandomChoice" ) >]
2007+ let randomChoice ( source : 'T array ) : 'T =
2008+ randomChoiceWith ThreadSafeRandom.Shared source
2009+
2010+ [<CompiledName( " RandomChoicesWith" ) >]
2011+ let randomChoicesWith ( random : Random ) ( count : int ) ( source : 'T array ) : 'T array =
2012+ checkNonNull " random" random
2013+ checkNonNull " source" source
2014+
2015+ if count < 0 then
2016+ invalidArgInputMustBeNonNegative " count" count
2017+
2018+ let inputLength = source.Length
2019+
2020+ if inputLength = 0 then
2021+ invalidArg " source" LanguagePrimitives.ErrorStrings.InputArrayEmptyString
2022+
2023+ let result = Microsoft.FSharp.Primitives.Basics.Array.zeroCreateUnchecked count
2024+
2025+ for i = 0 to count - 1 do
2026+ let j = random.Next( 0 , inputLength)
2027+ result[ i] <- source[ j]
2028+
2029+ result
2030+
2031+ [<CompiledName( " RandomChoicesBy" ) >]
2032+ let randomChoicesBy ( randomizer : unit -> float ) ( count : int ) ( source : 'T array ) : 'T array =
2033+ checkNonNull " source" source
2034+
2035+ if count < 0 then
2036+ invalidArgInputMustBeNonNegative " count" count
2037+
2038+ let inputLength = source.Length
2039+
2040+ if inputLength = 0 then
2041+ invalidArg " source" LanguagePrimitives.ErrorStrings.InputArrayEmptyString
2042+
2043+ let result = Microsoft.FSharp.Primitives.Basics.Array.zeroCreateUnchecked count
2044+
2045+ for i = 0 to count - 1 do
2046+ let j = Microsoft.FSharp.Primitives.Basics.Random.next randomizer 0 inputLength
2047+ result[ i] <- source[ j]
2048+
2049+ result
2050+
2051+ [<CompiledName( " RandomChoices" ) >]
2052+ let randomChoices ( count : int ) ( source : 'T array ) : 'T array =
2053+ randomChoicesWith ThreadSafeRandom.Shared count source
2054+
2055+ [<CompiledName( " RandomSampleWith" ) >]
2056+ let randomSampleWith ( random : Random ) ( count : int ) ( source : 'T array ) : 'T array =
2057+ checkNonNull " random" random
2058+ checkNonNull " source" source
2059+
2060+ if count < 0 then
2061+ invalidArgInputMustBeNonNegative " count" count
2062+
2063+ let inputLength = source.Length
2064+
2065+ if inputLength = 0 then
2066+ invalidArg " source" LanguagePrimitives.ErrorStrings.InputArrayEmptyString
2067+
2068+ if count > inputLength then
2069+ invalidArg " count" ( SR.GetString( SR.notEnoughElements))
2070+
2071+ let result = Microsoft.FSharp.Primitives.Basics.Array.zeroCreateUnchecked count
2072+
2073+ let setSize =
2074+ Microsoft.FSharp.Primitives.Basics.Random.getMaxSetSizeForSampling count
2075+
2076+ if inputLength <= setSize then
2077+ let pool = copy source
2078+
2079+ for i = 0 to count - 1 do
2080+ let j = random.Next( 0 , inputLength - i)
2081+ result[ i] <- pool[ j]
2082+ pool[ j] <- pool[ inputLength - i - 1 ]
2083+ else
2084+ let selected = HashSet()
2085+
2086+ for i = 0 to count - 1 do
2087+ let mutable j = random.Next( 0 , inputLength)
2088+
2089+ while not ( selected.Add j) do
2090+ j <- random.Next( 0 , inputLength)
2091+
2092+ result[ i] <- source[ j]
2093+
2094+ result
2095+
2096+ [<CompiledName( " RandomSampleBy" ) >]
2097+ let randomSampleBy ( randomizer : unit -> float ) ( count : int ) ( source : 'T array ) : 'T array =
2098+ checkNonNull " source" source
2099+
2100+ if count < 0 then
2101+ invalidArgInputMustBeNonNegative " count" count
2102+
2103+ let inputLength = source.Length
2104+
2105+ if inputLength = 0 then
2106+ invalidArg " source" LanguagePrimitives.ErrorStrings.InputArrayEmptyString
2107+
2108+ if count > inputLength then
2109+ invalidArg " count" ( SR.GetString( SR.notEnoughElements))
2110+
2111+ let result = Microsoft.FSharp.Primitives.Basics.Array.zeroCreateUnchecked count
2112+
2113+ // algorithm taken from https://github.com/python/cpython/blob/69b3e8ea569faabccd74036e3d0e5ec7c0c62a20/Lib/random.py#L363-L456
2114+ let setSize =
2115+ Microsoft.FSharp.Primitives.Basics.Random.getMaxSetSizeForSampling count
2116+
2117+ if inputLength <= setSize then
2118+ let pool = copy source
2119+
2120+ for i = 0 to count - 1 do
2121+ let j =
2122+ Microsoft.FSharp.Primitives.Basics.Random.next randomizer 0 ( inputLength - i)
2123+
2124+ result[ i] <- pool[ j]
2125+ pool[ j] <- pool[ inputLength - i - 1 ]
2126+ else
2127+ let selected = HashSet()
2128+
2129+ for i = 0 to count - 1 do
2130+ let mutable j =
2131+ Microsoft.FSharp.Primitives.Basics.Random.next randomizer 0 inputLength
2132+
2133+ while not ( selected.Add j) do
2134+ j <- Microsoft.FSharp.Primitives.Basics.Random.next randomizer 0 inputLength
2135+
2136+ result[ i] <- source[ j]
2137+
2138+ result
2139+
2140+ [<CompiledName( " RandomSample" ) >]
2141+ let randomSample ( count : int ) ( source : 'T array ) : 'T array =
2142+ randomSampleWith ThreadSafeRandom.Shared count source
2143+
19392144 module Parallel =
19402145 open System.Threading
19412146 open System.Threading .Tasks
0 commit comments