MCPcopy Create free account
hub / github.com/Effect-TS/effect / shuffle

Function shuffle

packages/effect/src/Random.ts:206–216  ·  view source on GitHub ↗
(elements: Iterable<A>)

Source from the content-addressed store, hash-verified

204 * @since 2.0.0
205 */
206export const shuffle = <A>(elements: Iterable<A>): Effect.Effect<Array<A>> =>
207 randomWith((r) => {
208 const buffer = Array.from(elements)
209 for (let i = buffer.length - 1; i >= 1; i = i - 1) {
210 const index = Math.min(i, Math.floor(r.nextDoubleUnsafe() * (i + 1)))
211 const value = buffer[i]!
212 buffer[i] = buffer[index]!
213 buffer[index] = value
214 }
215 return buffer
216 })
217
218/**
219 * Gets a random element from an iterable.

Callers

nothing calls this directly

Calls 2

randomWithFunction · 0.85
nextDoubleUnsafeMethod · 0.65

Tested by

no test coverage detected