MathGroup Archive 2004

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

Search the Archive

Re: shuffling (randomizing) a series

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

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 netvision.net.il)
> 
> 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:

http://forums.wolfram.com/mathgroup/archive/2001/Jan/msg00087.html

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

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.

http://library.wolfram.com/infocenter/Conferences/325/

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

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