Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Array shuffle function in random module #5183

Closed
vicuna opened this issue Nov 29, 2010 · 1 comment
Closed

Array shuffle function in random module #5183

vicuna opened this issue Nov 29, 2010 · 1 comment

Comments

@vicuna
Copy link

vicuna commented Nov 29, 2010

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

@vicuna
Copy link
Author

vicuna commented Dec 7, 2016

Comment author: @alainfrisch

If someone still cares about it, please open a Github PR (with some tests). I personally believe this would be a useful addition.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants