Mantis Bug Tracker

View Issue Details Jump to Notes ] Issue History ] Print ]
IDProjectCategoryView StatusDate SubmittedLast Update
0005183OCamlOCaml generalpublic2010-11-29 15:032013-10-07 16:22
Reporterwarwick 
Assigned Todoligez 
PrioritynormalSeverityfeatureReproducibilityalways
StatusassignedResolutionopen 
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 = 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
Tagspatch
Attached Files

- Relationships

-  Notes
There are no notes attached to this issue.

- 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


Copyright © 2000 - 2011 MantisBT Group
Powered by Mantis Bugtracker