You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Original bug ID: 5183 Reporter: warwick Assigned to:@damiendoligez Status: resolved (set by @alainfrisch on 2016-12-07T16:04:11Z) Resolution: suspended Priority: normal Severity: feature Version: 3.12.0 Category: standard library Tags: patch Monitored by:@gasche
Bug description
It would be useful to include a function within the Random module to shuffle an array. I think this is a fairly fundamental operation and therefore worthy of inclusion in the OCaml standard library rather than having to download/install an extension module to do it.
It's also a function that's significantly error-prone to implement, so having a correct version in the standard library would be a handy so that individual programmers don't have to code this function from scratch in their applications.
Additional information
(** [shuffle a] shuffles an array, giving a uniform random distribution )
let shuffle a =
for i = pred (Array.length a) downto 1 do
let j = Random.int (succ i) in
if i <> j ( faster to omit this test with arrays of about 100000 elements or more *) then (
let tmp = Array.unsafe_get a i in
Array.unsafe_set a i (Array.unsafe_get a j);
Array.unsafe_set a j tmp
)
done
The text was updated successfully, but these errors were encountered:
Original bug ID: 5183
Reporter: warwick
Assigned to: @damiendoligez
Status: resolved (set by @alainfrisch on 2016-12-07T16:04:11Z)
Resolution: suspended
Priority: normal
Severity: feature
Version: 3.12.0
Category: standard library
Tags: patch
Monitored by: @gasche
Bug description
It would be useful to include a function within the Random module to shuffle an array. I think this is a fairly fundamental operation and therefore worthy of inclusion in the OCaml standard library rather than having to download/install an extension module to do it.
It's also a function that's significantly error-prone to implement, so having a correct version in the standard library would be a handy so that individual programmers don't have to code this function from scratch in their applications.
Additional information
(** [shuffle a] shuffles an array, giving a uniform random distribution )
let shuffle a =
for i = pred (Array.length a) downto 1 do
let j = Random.int (succ i) in
if i <> j ( faster to omit this test with arrays of about 100000 elements or more *) then (
let tmp = Array.unsafe_get a i in
Array.unsafe_set a i (Array.unsafe_get a j);
Array.unsafe_set a j tmp
)
done
The text was updated successfully, but these errors were encountered: