Services & Resources / Wolfram Forums
MathGroup Archive
*Archive Index
*Ask about this page
*Print this page
*Give us feedback
*Sign up for the Wolfram Insider

MathGroup Archive 2004

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

Search the Archive

Re: shuffling (randomizing) a series

  • To: mathgroup at
  • Subject: [mg52905] Re: [mg52857] shuffling (randomizing) a series
  • From: Daniel Lichtblau <danl at>
  • Date: Thu, 16 Dec 2004 03:40:56 -0500 (EST)
  • References: <>
  • Sender: owner-wri-mathgroup at

George Szpiro wrote:
> Hello,
> I would like to put a series of numbers (of the order of 10^7 numbers) into 
> random order, i.e., to shuffle it. In Excel I would simply add a column next 
> to the column with the data, and then sort by the random number. But I could 
> only do this for 64000 numbers. How can I do that in Mathematica for a file with a 
> much larger series?
> (could you please copy any answer to my address:
> george at
> Thanks!
> George

This comes up from time to time. One can do it in Mathematica using the 
method you describe.

Another method is shown in a MathGroup note at:

I believe both of these are in the DiscreteMath`Combinatorica add-on 

Here is a faster version of the one in that MathGroup note, suitable for 
shuffling the entire set (the code in the note was for "dealing" a 
subset of shuffled elements). This is taken from a notebook in our 
infocenter repository.

shuffleC = Compile[{{n, _Integer}},
	Module[{res = Range[n], tmp, rand},
		rand = Random[Integer, {j, n}];
		tmp = res[[j]];
		res[[j]] = res[[rand]];
		res[[rand]] = tmp,
		{j, 1, n}];

For example:

len = 10^7;
set = Table[Random[], {len}];

Timing[shuffledset = set[[shuffleC[len]]];]

Out[17]= {16. Second, Null}

Daniel Lichtblau
Wolfram Research

  • Prev by Date: Re: about Package
  • Next by Date: Re: Delete problem
  • Previous by thread: Re: Re: shuffling (randomizing) a series
  • Next by thread: Re: shuffling (randomizing) a series