Mantis Bug Tracker

View Issue Details Jump to Notes ] Issue History ] Print ]
IDProjectCategoryView StatusDate SubmittedLast Update
0005183OCamlstandard librarypublic2010-11-29 15:032016-12-07 17:04
Assigned Todoligez 
PlatformOSOS Version
Product Version3.12.0 
Target VersionFixed in Version 
Summary0005183: Array shuffle function in random module
DescriptionIt 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 = (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
Attached Files

- Relationships

-  Notes
frisch (developer)
2016-12-07 17:04

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

- Issue History
Date Modified Username Field Change
2010-11-29 15:03 warwick New Issue
2011-05-20 14:45 doligez Status new => assigned
2011-05-20 14:45 doligez Assigned To => doligez
2013-10-07 16:22 doligez Tag Attached: patch
2016-12-07 14:43 shinwell Category OCaml general => OCaml standard library
2016-12-07 17:04 frisch Note Added: 0016740
2016-12-07 17:04 frisch Status assigned => resolved
2016-12-07 17:04 frisch Resolution open => suspended
2017-02-23 16:43 doligez Category OCaml standard library => standard library

Copyright © 2000 - 2011 MantisBT Group
Powered by Mantis Bugtracker