MathGroup Archive 2001

[Date Index] [Thread Index] [Author Index]

Search the Archive

shuffling flaw, and MASH call-for-support

  • To: mathgroup at
  • Subject: [mg28475] shuffling flaw, and MASH call-for-support
  • From: Daniel Reeves <dreeves at>
  • Date: Fri, 20 Apr 2001 04:24:23 -0400 (EDT)
  • Sender: owner-wri-mathgroup at

> scramble[list_] := Sort[list, (Random[Integer] == 1) &]

I don't think that will always work, depending on the implementation of
In fact, here's a way to see that it doesn't:

Speaking of shuffling, below is a shell-/perl-script-style mathematica
program to shuffle the lines of text sent to it on stdin.
I'm trying to convince people at Wolfram that Mathematica should be able
to be used this way without a mathlink wrapper.  Every other "real
language" can do this so for Mathematica to be taken seriously by
programmers as a programming language, it needs to be able to do this too.
If you agree, please send me an email.  A simple "yeah, I might use that"
is sufficient.

(* A sample mathematica script -- the inverse of the
   unix sort utility.
   If a number is specified as a command line argument,
   returns only that many of the lines.
   So this can be used like RandomKSubset for files.
   Example usage:  shuffle.m 3 < test.txt | sort
     Takes 3 random lines from test.txt and outputs
     them in sorted order.
   (The functions readList, pout, and perr are defined
    in MathIO.m which is automatically loaded by mash.
    See )

(* FUNCTIONS ********************************************)

(* Shuffles a list. Inspired by RandomPermutation. *)
shuffle[l_List] := #2& @@@ Sort[{Random[],#}& /@ l]

(* MAIN *************************************************)

(* number of lines to output, specified on command line *)
numWanted = If[Length[ARGV]<2, Infinity,

  perr["Usage: ", ARGV[[1]], " [num of lines]\n";]];

(* gets a list of all the lines on stdin *)
lines = readList[];
numLines = Length[lines];

(* prints numWanted of them in random order to stdout *)
pout @@ Take[shuffle[lines], Min[numLines, numWanted]];

--    --    --    --    --    --    --    --    --    --    --    -- 
Daniel Reeves     

The goal of Computer Science is to build something that will last at
least until we've finished building it.

  • Prev by Date: Re: Summation Formulae Table
  • Next by Date: Echelon form of a matrix
  • Previous by thread: Re: considering reflections in ray tracing with optica
  • Next by thread: Echelon form of a matrix