comp.lang.idl-pvwave archive - RDF feed
type: channel
description: Messages from Usenet group comp.lang.idl-pvwave, compiled by Paulo Penteado
items: Re: Randomize array order

Re: Randomize array order
type: item
description: On Jul 25, 2:17 pm, Conor <cmanc...@gmail.com> wrote: > Hi everyone! > > Anyone know an efficient way to randomize an array (I have a > sorted array that I want unsorted). Initially, I tried something like > this: > > array = findgen(1000000) > unsort = array[sort(randomu(seed,1000000))] > > It works, but sorting on a million elements is rather slow. Anyone > know a faster way? Takes about 0.87 seconds on my machine? Is this too long? Of course, if you want "with replacement" you could do: unsort = array[long(1000000*randomu(seed,1000000))] But you probably want a permutation, right? That'll be more difficult.
creator: Vince Hradil
date: 2007-07-25T19:59:14-00:00

Re: Randomize array order
type: item
description: On Jul 25, 3:59 pm, hradilv <hrad...@yahoo.com> wrote: > On Jul 25, 2:17 pm, Conor <cmanc...@gmail.com> wrote: > >> Hi everyone! > >> Anyone know an efficient way to randomize an array (I have a >> sorted array that I want unsorted). Initially, I tried something like >> this: > >> array = findgen(1000000) >> unsort = array[sort(randomu(seed,1000000))] > >> It works, but sorting on a million elements is rather slow. Anyone >> know a faster way? > > Takes about 0.87 seconds on my machine? Is this too long? > > Of course, if you want "with replacement" you could do: > unsort = array[long(1000000*randomu(seed,1000000))] > > But you probably want a permutation, right? That'll be more difficult. It takes about 3 seconds on my machine (really crappy machine). The whole program takes about 6 seconds, so about half the program execution comes from the one sort statement. This is going to execute somewhere between 1000 and 10,000 times, so losing three seconds will save me between 50 minutes and 8 hours. Yeah, I need a permutation. Losing or repeating elements would be bad. It's always got to be the hard way, doesn't it :)
creator: Conor
date: 2007-07-26T12:24:55-00:00

Re: Randomize array order
type: item
description: On Jul 25, 3:59 pm, hradilv <hrad...@yahoo.com> wrote: > On Jul 25, 2:17 pm, Conor <cmanc...@gmail.com> wrote: > >> Hi everyone! > >> Anyone know an efficient way to randomize an array (I have a >> sorted array that I want unsorted). Initially, I tried something like >> this: > >> array = findgen(1000000) >> unsort = array[sort(randomu(seed,1000000))] > >> It works, but sorting on a million elements is rather slow. Anyone >> know a faster way? > > Takes about 0.87 seconds on my machine? Is this too long? > > Of course, if you want "with replacement" you could do: > unsort = array[long(1000000*randomu(seed,1000000))] > > But you probably want a permutation, right? That'll be more difficult. You know, it's a shame that sort and histogram don't make use of the thread pool. If there were any algorithms that need to be multi- threaded, those would be it. Oh well :(
creator: Conor
date: 2007-07-26T12:28:46-00:00

Re: Randomize array order
type: item
description: On Jul 25, 3:17 pm, Conor <cmanc...@gmail.com> wrote: > Hi everyone! > > Anyone know an efficient way to randomize an array (I have a > sorted array that I want unsorted). Initially, I tried something like > this: > > array = findgen(1000000) > unsort = array[sort(randomu(seed,1000000))] > > It works, but sorting on a million elements is rather slow. Anyone > know a faster way? I think this is one of those vectorization cases where you have a trade-off between processor time and memory-- the one 'obvious' faster way that occurs to me is to pre-compute a sequence of a few billion random integers-- and then just select sub-sequences as needed. It's possible that there's some intermediate strategy that's not quite so extravagantly wasteful of memory.
creator: mattf
date: 2007-07-26T12:50:25-00:00

Re: Randomize array order
type: item
description: for the without replacement see http://www.dfanning.com/code_tips/randomindex.html I have written a resample with and without replacement routine based on this and its works fine. Cheers, Brian ------------------------------------------------------------ --------------------- Brian Larsen Boston University Center for Space Physics
creator: Brian Larsen
date: 2007-07-26T13:04:19-00:00

Re: Randomize array order
type: item
description: for the without replacement see http://www.dfanning.com/code_tips/randomindex.html I have written a resample with and without replacement routine based on this and its works fine. Cheers, Brian ------------------------------------------------------------ --------------------- Brian Larsen Boston University Center for Space Physics
creator: Brian Larsen
date: 2007-07-26T13:06:21-00:00

Re: Randomize array order
type: item
description: Conor wrote: > Hi everyone! > > Anyone know an efficient way to randomize an array (I have a > sorted array that I want unsorted). Initially, I tried something like > this: > > array = findgen(1000000) > unsort = array[sort(randomu(seed,1000000))] > > It works, but sorting on a million elements is rather slow. Anyone > know a faster way? > Conor, Is it a million elements you want to do? The following scales better: pro shuffle,in b=long(n_elements(in)*randomu(seed,n_elements(in))) for i=0l,n_elements(in)-1 do begin tmp=in[i] in[i]=in[b[i]] in[b[i]]=tmp end end but on my machine, a million elements is around about where it starts to become as efficient as yours. For 10 million elements the above is a bit (17.05 seconds vs 12.92 seconds) but for 1 million elements they both come in at around 1.2 seconds (1.15 seconds vs 1.26 seconds). The above will scale as pretty much O(n) since it doesn't do any sorting but it takes a hit in the practical implementation because of the loop in IDL-space. Your suggestion will scale worse than O(n) but it seems the overlap in the two methods is exactly where you want to work. Maybe my loop can be made more efficient in practical terms but I don't think this is any better algorithm in terms of scaling (hard to imagine anything that could go faster than O(n) to randomise n things). Probably not helpful but I thought it was interesting that the cross-over is exactly where you want to work. But, maybe I should get out more if I think that's especially interesting. Thanks, Allan
creator: Allan Whiteford
date: 2007-07-26T13:30:00-00:00

Re: Randomize array order
type: item
description: On Jul 26, 9:30 am, Allan Whiteford <allan.rem...@phys.remove.strath.ac.remove.uk> wrote: > Conor wrote: >> Hi everyone! > >> Anyone know an efficient way to randomize an array (I have a >> sorted array that I want unsorted). Initially, I tried something like >> this: > >> array = findgen(1000000) >> unsort = array[sort(randomu(seed,1000000))] > >> It works, but sorting on a million elements is rather slow. Anyone >> know a faster way? > > Conor, > > Is it a million elements you want to do? > > The following scales better: > > pro shuffle,in > b=long(n_elements(in)*randomu(seed,n_elements(in))) > for i=0l,n_elements(in)-1 do begin > tmp=in[i] > in[i]=in[b[i]] > in[b[i]]=tmp > end > end > > but on my machine, a million elements is around about where it starts to > become as efficient as yours. For 10 million elements the above is a bit > (17.05 seconds vs 12.92 seconds) but for 1 million elements they both > come in at around 1.2 seconds (1.15 seconds vs 1.26 seconds). The above > will scale as pretty much O(n) since it doesn't do any sorting but it > takes a hit in the practical implementation because of the loop in > IDL-space. Your suggestion will scale worse than O(n) but it seems the > overlap in the two methods is exactly where you want to work. > > Maybe my loop can be made more efficient in practical terms but I don't > think this is any better algorithm in terms of scaling (hard to imagine > anything that could go faster than O(n) to randomise n things). > > Probably not helpful but I thought it was interesting that the > cross-over is exactly where you want to work. But, maybe I should get > out more if I think that's especially interesting. > > Thanks, > > Allan Thanks for the suggestions guys! I'll have to play around and see what works best.
creator: Conor
date: 2007-07-26T13:40:56-00:00

Re: Randomize array order
type: item
description: On Jul 26, 8:40 am, Conor <cmanc...@gmail.com> wrote: > On Jul 26, 9:30 am, Allan Whiteford > > > > <allan.rem...@phys.remove.strath.ac.remove.uk> wrote: >> Conor wrote: >>> Hi everyone! > >>> Anyone know an efficient way to randomize an array (I have a >>> sorted array that I want unsorted). Initially, I tried something like >>> this: > >>> array = findgen(1000000) >>> unsort = array[sort(randomu(seed,1000000))] > >>> It works, but sorting on a million elements is rather slow. Anyone >>> know a faster way? > >> Conor, > >> Is it a million elements you want to do? > >> The following scales better: > >> pro shuffle,in >> b=long(n_elements(in)*randomu(seed,n_elements(in))) >> for i=0l,n_elements(in)-1 do begin >> tmp=in[i] >> in[i]=in[b[i]] >> in[b[i]]=tmp >> end >> end > >> but on my machine, a million elements is around about where it starts to >> become as efficient as yours. For 10 million elements the above is a bit >> (17.05 seconds vs 12.92 seconds) but for 1 million elements they both >> come in at around 1.2 seconds (1.15 seconds vs 1.26 seconds). The above >> will scale as pretty much O(n) since it doesn't do any sorting but it >> takes a hit in the practical implementation because of the loop in >> IDL-space. Your suggestion will scale worse than O(n) but it seems the >> overlap in the two methods is exactly where you want to work. > >> Maybe my loop can be made more efficient in practical terms but I don't >> think this is any better algorithm in terms of scaling (hard to imagine >> anything that could go faster than O(n) to randomise n things). > >> Probably not helpful but I thought it was interesting that the >> cross-over is exactly where you want to work. But, maybe I should get >> out more if I think that's especially interesting. > >> Thanks, > >> Allan > > Thanks for the suggestions guys! I'll have to play around and see > what works best. Here's a table of results from my machine. All times are in seconds. PC single processor, WinXP, IDL6.4 i Nite…
creator: Vince Hradil
date: 2007-07-26T14:58:19-00:00

Re: Randomize array order
type: item
description: On Jul 26, 9:58 am, hradilv <hrad...@yahoo.com> wrote: > On Jul 26, 8:40 am, Conor <cmanc...@gmail.com> wrote: > > > >> On Jul 26, 9:30 am, Allan Whiteford > >> <allan.rem...@phys.remove.strath.ac.remove.uk> wrote: >>> Conor wrote: >>>> Hi everyone! > >>>> Anyone know an efficient way to randomize an array (I have a >>>> sorted array that I want unsorted). Initially, I tried something like >>>> this: > >>>> array = findgen(1000000) >>>> unsort = array[sort(randomu(seed,1000000))] > >>>> It works, but sorting on a million elements is rather slow. Anyone >>>> know a faster way? > >>> Conor, > >>> Is it a million elements you want to do? > >>> The following scales better: > >>> pro shuffle,in >>> b=long(n_elements(in)*randomu(seed,n_elements(in))) >>> for i=0l,n_elements(in)-1 do begin >>> tmp=in[i] >>> in[i]=in[b[i]] >>> in[b[i]]=tmp >>> end >>> end > >>> but on my machine, a million elements is around about where it starts to >>> become as efficient as yours. For 10 million elements the above is a bit >>> (17.05 seconds vs 12.92 seconds) but for 1 million elements they both >>> come in at around 1.2 seconds (1.15 seconds vs 1.26 seconds). The above >>> will scale as pretty much O(n) since it doesn't do any sorting but it >>> takes a hit in the practical implementation because of the loop in >>> IDL-space. Your suggestion will scale worse than O(n) but it seems the >>> overlap in the two methods is exactly where you want to work. > >>> Maybe my loop can be made more efficient in practical terms but I don't >>> think this is any better algorithm in terms of scaling (hard to imagine >>> anything that could go faster than O(n) to randomise n things). > >>> Probably not helpful but I thought it was interesting that the >>> cross-over is exactly where you want to work. But, maybe I should get >>> out more if I think that's especially interesting. > >>> Thanks, > >>> Allan > >> Thanks for the suggestions guys! I'll have to play around and see >> what works best. > > Here's …
creator: Vince Hradil
date: 2007-07-26T15:13:36-00:00

Re: Randomize array order
type: item
description: hradilv wrote: > On Jul 26, 9:58 am, hradilv <hrad...@yahoo.com> wrote: > >> On Jul 26, 8:40 am, Conor <cmanc...@gmail.com> wrote: >> >> >> >> >>> On Jul 26, 9:30 am, Allan Whiteford >> >>> <allan.rem...@phys.remove.strath.ac.remove.uk> wrote: >>> >>>> Conor wrote: >>>> >>>> >Hi everyone! >> >>>> > Anyone know an efficient way to randomize an array (I have a >>>> >sorted array that I want unsorted). Initially, I tried something like >>>> >this: >> >>>> >array = findgen(1000000) >>>> >unsort = array[sort(randomu(seed,1000000))] >> >>>> >It works, but sorting on a million elements is rather slow. Anyone >>>> >know a faster way? >> >>>> Conor, >> >>>> Is it a million elements you want to do? >> >>>> The following scales better: >> >>>> pro shuffle,in >>>> b=long(n_elements(in)*randomu(seed,n_elements(in))) >>>> for i=0l,n_elements(in)-1 do begin >>>> tmp=in[i] >>>> in[i]=in[b[i]] >>>> in[b[i]]=tmp >>>> end >>>> end >> >>>> but on my machine, a million elements is around about where it starts to >>>> become as efficient as yours. For 10 million elements the above is a bit >>>> (17.05 seconds vs 12.92 seconds) but for 1 million elements they both >>>> come in at around 1.2 seconds (1.15 seconds vs 1.26 seconds). The above >>>> will scale as pretty much O(n) since it doesn't do any sorting but it >>>> takes a hit in the practical implementation because of the loop in >>>> IDL-space. Your suggestion will scale worse than O(n) but it seems the >>>> overlap in the two methods is exactly where you want to work. >> >>>> Maybe my loop can be made more efficient in practical terms but I don't >>>> think this is any better algorithm in terms of scaling (hard to imagine >>>> anything that could go faster than O(n) to randomise n things). >> >>>> Probably not helpful but I thought it was interesting that the >>>> cross-over is exactly where you want to work. But, maybe I should get >>>> out more if I think that's especially interesting. >> >>>> Thanks, >> >>>> Allan >> >>> Thanks f…
creator: Allan Whiteford
date: 2007-07-26T15:49:13-00:00

Re: Randomize array order
type: item
description: On Jul 26, 11:49 am, Allan Whiteford <allan.rem...@phys.remove.strath.ac.remove.uk> wrote: > hradilv wrote: >> On Jul 26, 9:58 am, hradilv <hrad...@yahoo.com> wrote: > >>> On Jul 26, 8:40 am, Conor <cmanc...@gmail.com> wrote: > >>>> On Jul 26, 9:30 am, Allan Whiteford > >>>> <allan.rem...@phys.remove.strath.ac.remove.uk> wrote: > >>>> >Conor wrote: > >>>> >>Hi everyone! > >>>> >> Anyone know an efficient way to randomize an array (I have a >>>> >>sorted array that I want unsorted). Initially, I tried something like >>>> >>this: > >>>> >>array = findgen(1000000) >>>> >>unsort = array[sort(randomu(seed,1000000))] > >>>> >>It works, but sorting on a million elements is rather slow. Anyone >>>> >>know a faster way? > >>>> >Conor, > >>>> >Is it a million elements you want to do? > >>>> >The following scales better: > >>>> >pro shuffle,in >>>> > b=long(n_elements(in)*randomu(seed,n_elements(in))) >>>> > for i=0l,n_elements(in)-1 do begin >>>> > tmp=in[i] >>>> > in[i]=in[b[i]] >>>> > in[b[i]]=tmp >>>> > end >>>> >end > >>>> >but on my machine, a million elements is around about where it starts to >>>> >become as efficient as yours. For 10 million elements the above is a bit >>>> >(17.05 seconds vs 12.92 seconds) but for 1 million elements they both >>>> >come in at around 1.2 seconds (1.15 seconds vs 1.26 seconds). The above >>>> >will scale as pretty much O(n) since it doesn't do any sorting but it >>>> >takes a hit in the practical implementation because of the loop in >>>> >IDL-space. Your suggestion will scale worse than O(n) but it seems the >>>> >overlap in the two methods is exactly where you want to work. > >>>> >Maybe my loop can be made more efficient in practical terms but I don't >>>> >think this is any better algorithm in terms of scaling (hard to imagine >>>> >anything that could go faster than O(n) to randomise n things). > >>>> >Probably not helpful but I thought it was interesting that the >>>> >cross-over is exactly where you want to work. But, maybe I sh…
creator: Conor
date: 2007-07-26T16:22:27-00:00

Re: Randomize array order
type: item
description: How about a Knuth shuffle? (Disclaimer: I'm not a statistician; I just found it on Wikipedia.) function kunsort, array na = n_elements(array) random = randomu(seed,na) * (na - array - 1) + array for i=0L,na-2 do array[i] = array[random[i]] return, array end Added to Vince's code (last column): 0 100000 0.0619998 0.0780001 0.0320001 1 166810 0.125000 0.125000 0.0780001 2 278256 0.282000 0.218000 0.141000 3 464158 0.547000 0.406000 0.235000 4 774263 1.07800 0.657000 0.390000 5 1291549 1.93700 1.09400 0.703000 6 2154435 3.51500 1.89100 1.20300 7 3593812 6.34400 3.11000 1.98400 8 5994841 11.5470 5.21800 3.36000 9 10000000 20.3750 8.67200 5.60900 Windows XP, dual 2.66 GHz, 3 GB RAM, IDL 6.3
creator: David Streutker
date: 2007-07-26T16:57:31-00:00

Re: Randomize array order
type: item
description: On Jul 26, 11:57 am, David Streutker <dstreut...@gmail.com> wrote: > How about a Knuth shuffle? > > (Disclaimer: I'm not a statistician; I just found it on Wikipedia.) > > function kunsort, array > na = n_elements(array) > random = randomu(seed,na) * (na - array - 1) + array > for i=0L,na-2 do array[i] = array[random[i]] > return, array > end > > Added to Vince's code (last column): > > 0 100000 0.0619998 0.0780001 0.0320001 > 1 166810 0.125000 0.125000 0.0780001 > 2 278256 0.282000 0.218000 0.141000 > 3 464158 0.547000 0.406000 0.235000 > 4 774263 1.07800 0.657000 0.390000 > 5 1291549 1.93700 1.09400 0.703000 > 6 2154435 3.51500 1.89100 1.20300 > 7 3593812 6.34400 3.11000 1.98400 > 8 5994841 11.5470 5.21800 3.36000 > 9 10000000 20.3750 8.67200 5.60900 > > Windows XP, dual 2.66 GHz, 3 GB RAM, IDL 6.3 I'm not sure, but I think that will give you "with replacement".
creator: Vince Hradil
date: 2007-07-26T20:32:39-00:00

Re: Randomize array order
type: item
description: On Jul 26, 2:32 pm, hradilv <hrad...@yahoo.com> wrote: > On Jul 26, 11:57 am, David Streutker <dstreut...@gmail.com> wrote: > > > >> How about a Knuth shuffle? > >> (Disclaimer: I'm not a statistician; I just found it on Wikipedia.) > >> function kunsort, array >> na = n_elements(array) >> random = randomu(seed,na) * (na - array - 1) + array >> for i=0L,na-2 do array[i] = array[random[i]] >> return, array >> end > >> Added to Vince's code (last column): > >> 0 100000 0.0619998 0.0780001 0.0320001 >> 1 166810 0.125000 0.125000 0.0780001 >> 2 278256 0.282000 0.218000 0.141000 >> 3 464158 0.547000 0.406000 0.235000 >> 4 774263 1.07800 0.657000 0.390000 >> 5 1291549 1.93700 1.09400 0.703000 >> 6 2154435 3.51500 1.89100 1.20300 >> 7 3593812 6.34400 3.11000 1.98400 >> 8 5994841 11.5470 5.21800 3.36000 >> 9 10000000 20.3750 8.67200 5.60900 > >> Windows XP, dual 2.66 GHz, 3 GB RAM, IDL 6.3 > > I'm not sure, but I think that will give you "with replacement". You're right, I wasn't swapping. Corrected, and in the form of Allan's method: function kunsort, array na = n_elements(array) rarray = array b = randomu(seed,na-1) * (na - lindgen(na-1) - 1) + lindgen(na-1) for i=0L,na-2 do begin tmp = rarray[i] rarray[i] = rarray[b[i]] rarray[b[i]] = tmp endfor return, rarray end With the change, it's slightly slower than Allan's. However, for what it's worth, there are claims this is a less biased method. (Again, I am no expert. But the recent poker craze seems to have revived interest in the probabilities of shuffling.)
creator: David Streutker
date: 2007-07-26T21:27:33-00:00

Re: Randomize array order
type: item
description: David Streutker wrote: > On Jul 26, 2:32 pm, hradilv <hrad...@yahoo.com> wrote: > >> On Jul 26, 11:57 am, David Streutker <dstreut...@gmail.com> wrote: >> >> >> >> >>> How about a Knuth shuffle? >> >>> (Disclaimer: I'm not a statistician; I just found it on Wikipedia.) >> >>> function kunsort, array >>> na = n_elements(array) >>> random = randomu(seed,na) * (na - array - 1) + array >>> for i=0L,na-2 do array[i] = array[random[i]] >>> return, array >>> end >> >>> Added to Vince's code (last column): >> >>> 0 100000 0.0619998 0.0780001 0.0320001 >>> 1 166810 0.125000 0.125000 0.0780001 >>> 2 278256 0.282000 0.218000 0.141000 >>> 3 464158 0.547000 0.406000 0.235000 >>> 4 774263 1.07800 0.657000 0.390000 >>> 5 1291549 1.93700 1.09400 0.703000 >>> 6 2154435 3.51500 1.89100 1.20300 >>> 7 3593812 6.34400 3.11000 1.98400 >>> 8 5994841 11.5470 5.21800 3.36000 >>> 9 10000000 20.3750 8.67200 5.60900 >> >>> Windows XP, dual 2.66 GHz, 3 GB RAM, IDL 6.3 >> >> I'm not sure, but I think that will give you "with replacement". > > > You're right, I wasn't swapping. Corrected, and in the form of > Allan's method: > > function kunsort, array > na = n_elements(array) > rarray = array > > b = randomu(seed,na-1) * (na - lindgen(na-1) - 1) + lindgen(na-1) > for i=0L,na-2 do begin > tmp = rarray[i] > rarray[i] = rarray[b[i]] > rarray[b[i]] = tmp > endfor > > return, rarray > end > > With the change, it's slightly slower than Allan's. However, for what > it's worth, there are claims this is a less biased method. (Again, I > am no expert. But the recent poker craze seems to have revived > interest in the probabilities of shuffling.) > Yours is doing a bit more than mine was in that it's creating a copy of the original data rather than in-place swapping so that would make yours a bit slower (but probably more useful). You can probably also get a speed up by converting "b" to a long at creation time rather than implicitly ever time you use it. Looking over what a Knuth Shuffle is supposed to to…
creator: Allan Whiteford
date: 2007-07-27T09:33:30-00:00

Re: Randomize array order
type: item
description: Conor wrote: > Hi everyone! > > Anyone know an efficient way to randomize an array (I have a > sorted array that I want unsorted). Initially, I tried something like > this: > > array = findgen(1000000) > unsort = array[sort(randomu(seed,1000000))] > > It works, but sorting on a million elements is rather slow. Anyone > know a faster way? > Slightly different point and probably a bit academic: If you have a million elements then you have 1000000! (i.e. one million factorial) different ways to re-order the data. However, your seed is a 4 byte integer which can only take 2^32 different values. Some messing about suggests that: 1000000! =~ 10^5568636 which means there are ~ 10^5568636 different ways to re-arrange your elements as opposed to the 4 x 10^9 values your seed can take. Thus, using any of the algorithms suggested you're only going to sample 10^-5568625 % of the possible values. This is a really small number. It means that no matter how hard you try and how many times you do things you'll never be able to access anything but a tiny number of the possibilities without doing multiple shufflings - I think it's something like 618737 sub-shufflings (i.e. 5568636 / 9) but that could be wrong. However, that requires producing 618737 seeds per major-shuffle (and you can't use a generator based on a 4 byte seed to produce these seeds). But, since you're only going to be running the code 1000-10,000 times (which is much smaller than 4e9) I guess everything will be ok. I don't know if anyone has studied possible correlations of results as a function of the very small number of seeds (compared to the data), whatever random number generator is used and the shuffling method. Presumably they have and presumably everything is ok. Does anyone know? Thanks, Allan
creator: Allan Whiteford
date: 2007-07-27T12:03:34-00:00

Re: Randomize array order
type: item
description: Allan Whiteford wrote: > Conor wrote: >> Hi everyone! >> >> Anyone know an efficient way to randomize an array (I have a >> sorted array that I want unsorted). Initially, I tried something like >> this: >> >> array = findgen(1000000) >> unsort = array[sort(randomu(seed,1000000))] >> >> It works, but sorting on a million elements is rather slow. Anyone >> know a faster way? >> > > Slightly different point and probably a bit academic: > > If you have a million elements then you have 1000000! (i.e. one million > factorial) different ways to re-order the data. However, your seed is a > 4 byte integer which can only take 2^32 different values. > > Some messing about suggests that: > > 1000000! =~ 10^5568636 > > which means there are ~ 10^5568636 different ways to re-arrange your > elements as opposed to the 4 x 10^9 values your seed can take. > > Thus, using any of the algorithms suggested you're only going to sample > > 10^-5568625 % > > of the possible values. This is a really small number. It means that no > matter how hard you try and how many times you do things you'll never be > able to access anything but a tiny number of the possibilities without > doing multiple shufflings - I think it's something like 618737 > sub-shufflings (i.e. 5568636 / 9) but that could be wrong. However, that > requires producing 618737 seeds per major-shuffle (and you can't use a > generator based on a 4 byte seed to produce these seeds). > > But, since you're only going to be running the code 1000-10,000 times > (which is much smaller than 4e9) I am by no means an expert, but I think that in general, common sense suggests not to use more random numbers for one project than the cycle length of the generator (that is, the length at which the numbers start to repeat themselves). From the docs is hard to guess how long the cycle is, but it can be at most 2^32 for a generator using long ints. So I wouldn't suggest doing more than 2^32/1d6 runs of the code, if one is using 1d6 numbers per run…
creator: Paolo Grigis
date: 2007-07-27T13:59:08-00:00

Re: Randomize array order
type: item
description: On Jul 27, 6:03 am, Allan Whiteford <allan.rem...@phys.remove.strath.ac.remove.uk> wrote: > Conor wrote: >> Hi everyone! > >> Anyone know an efficient way to randomize an array (I have a >> sorted array that I want unsorted). Initially, I tried something like >> this: > >> array = findgen(1000000) >> unsort = array[sort(randomu(seed,1000000))] > >> It works, but sorting on a million elements is rather slow. Anyone >> know a faster way? > > Slightly different point and probably a bit academic: > > If you have a million elements then you have 1000000! (i.e. one million > factorial) different ways to re-order the data. However, your seed is a > 4 byte integer which can only take 2^32 different values. > > Some messing about suggests that: > > 1000000! =~ 10^5568636 > > which means there are ~ 10^5568636 different ways to re-arrange your > elements as opposed to the 4 x 10^9 values your seed can take. > > Thus, using any of the algorithms suggested you're only going to sample > > 10^-5568625 % > > of the possible values. This is a really small number. It means that no > matter how hard you try and how many times you do things you'll never be > able to access anything but a tiny number of the possibilities without > doing multiple shufflings - I think it's something like 618737 > sub-shufflings (i.e. 5568636 / 9) but that could be wrong. However, that > requires producing 618737 seeds per major-shuffle (and you can't use a > generator based on a 4 byte seed to produce these seeds). > > But, since you're only going to be running the code 1000-10,000 times > (which is much smaller than 4e9) I guess everything will be ok. I don't > know if anyone has studied possible correlations of results as a > function of the very small number of seeds (compared to the data), > whatever random number generator is used and the shuffling method. > Presumably they have and presumably everything is ok. Does anyone know? > > Thanks, > > Allan I'm not sure that I agree. Where in any of our al…
creator: David Streutker
date: 2007-07-27T14:54:55-00:00

Re: Randomize array order
type: item
description: David Streutker wrote: > On Jul 27, 6:03 am, Allan Whiteford ... >> If you have a million elements then you have 1000000! (i.e. one million >> factorial) different ways to re-order the data. However, your seed is a >> 4 byte integer which can only take 2^32 different values. >> >> Some messing about suggests that: >> >> 1000000! =~ 10^5568636 >> >> which means there are ~ 10^5568636 different ways to re-arrange your >> elements as opposed to the 4 x 10^9 values your seed can take. >> >> Thus, using any of the algorithms suggested you're only going to sample >> >> 10^-5568625 % >> >> of the possible values. This is a really small number. It means that no >> matter how hard you try and how many times you do things you'll never be >> able to access anything but a tiny number of the possibilities without >> doing multiple shufflings - I think it's something like 618737 >> sub-shufflings (i.e. 5568636 / 9) but that could be wrong. However, that >> requires producing 618737 seeds per major-shuffle (and you can't use a >> generator based on a 4 byte seed to produce these seeds). >> >> But, since you're only going to be running the code 1000-10,000 times >> (which is much smaller than 4e9) I guess everything will be ok. I don't >> know if anyone has studied possible correlations of results as a >> function of the very small number of seeds (compared to the data), >> whatever random number generator is used and the shuffling method. >> Presumably they have and presumably everything is ok. Does anyone know? >> >> Thanks, >> >> Allan > > I'm not sure that I agree. Where in any of our algorithms are we > unable to access a (theoretically) possible outcome? As long as we > are able to randomly select any element of the array in each step, it > should work, right? (I.e., as long as the input array has fewer than > 2^32 elements.) In your analysis, shouldn't we be using (2^32)^n for > the maximum possible number of randomly generated combinations, where > n is the number of steps/…
creator: James Kuyper
date: 2007-07-27T16:05:27-00:00