MathGroup Archive 2001

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

Search the Archive

shuffling flaw, and MASH call-for-support

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

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

I don't think that will always work, depending on the implementation of
Sort...
In fact, here's a way to see that it doesn't:
ListPlot[scramble[Range[100000]]];
vs
ListPlot[RandomPermutation[100000]];

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.

#!mash
(* 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 http://ai.eecs.umich.edu/people/dreeves/mash )
*)

(* 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,
               ToExpression[ARGV[[2]]]];

If[Length[ARGV]>2,
  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               http://ai.eecs.umich.edu/people/dreeves/

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